-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.js
62 lines (46 loc) · 1.67 KB
/
channel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// You will have to do:
// const { Multiplexer } = require("redis-mpx");
const { Multiplexer } = require("..");
const redis = require('redis');
const WebSocket = require('ws');
let publish = redis.createClient({retry_strategy: () => 100});
let mpx = new Multiplexer();
let server = new WebSocket.Server({ port: 8080 });
server.on('connection', function connection(ws) {
let sub = mpx.createChannelSubscription(
(channel, message) => ws.send(`ch: [${channel}] msg: [${message}]`),
() => console.log("Disconnected!"),
(c) => console.log("Channel active:", c.toString())
);
var send_to_channel = undefined;
ws.on('close', function () {
console.log("[ws] closing");
sub.close();
});
ws.on('message', function incoming(message) {
if (message.length == 0) {
return
}
if (send_to_channel) {
publish.publish(send_to_channel, message);
send_to_channel = undefined;
}
let command = message[0];
let channel = message.slice(1);
switch(command) {
case '+':
sub.add(channel);
break;
case '-':
sub.remove(channel);
break;
case '!':
send_to_channel = channel;
break;
}
});
ws.send("// Sending `+hello` will subscribe you to channel `hello`, while `-hello` will do the opposite.");
ws.send("// Sending `!hello` will broadcast the next message you send to `hello`.");
ws.send("// You can subscribe to more channels than one.");
ws.send("// If the server loses connection with Redis, it will automatically try to reconnect.");
});