-
Notifications
You must be signed in to change notification settings - Fork 1
/
rx-server-offline.js
109 lines (96 loc) · 2.63 KB
/
rx-server-offline.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const dotenv = require('dotenv').config();
const express = require('express');
const http = require('http');
const redis = require('redis');
const Rx = require('rxjs');
const WebSocket = require('ws');
const app = express();
const server = http.createServer(app);
const {
EXPRESS_PORT,
REDIS_HOST,
REDIS_PORT,
REDIS_PASS } = process.env;
const redisSub = redis.createClient();
const redisPub = redis.createClient();
const connection = Rx.Observable.create(observer => {
const wss = new WebSocket.Server({ server });
wss.on('connection', ws => {
const closeMsg = () => {
observer.next({ ws, message: JSON.stringify({ action: 'WS_CLOSE' })})
};
ws.on('message', message => {
observer.next({ws, message});
})
ws.on('error', closeMsg);
ws.on('close', closeMsg);
});
});
const CHANNELS = new Map();
const addChannels = (channels, ws) => {
channels.forEach(channel => {
let wsConnections = CHANNELS.get(channel);
if(!wsConnections) {
wsConnections = new Set();
CHANNELS.set(channel, wsConnections);
}
wsConnections.add(ws);
});
return Array.from(CHANNELS.keys());
}
const deleteWSFromChannel = (ws) => {
CHANNELS.forEach((wsConnections, channel) => {
wsConnections.delete(ws);
});
}
redisSub.on('message', (channel, message) => {
console.log(`> Redis message 📩: ${message}`);
const wsConnections = CHANNELS.get(channel);
if(wsConnections) {
wsConnections.forEach(ws => {
ws.send(JSON.stringify({
action: 'SUBSCRIBEMSG',
channel,
message
}));
});
}
});
connection.subscribe(connection => {
try {
const message = JSON.parse(connection.message);
switch(message.action) {
case 'SUBSCRIBE':
redisSub.subscribe(
addChannels(message.channels, connection.ws).join(' ')
);
console.log(`> SUBSCRIBE to: [${message.channels}]`);
break;
case 'PUBLISH':
redisSub.subscribe(
addChannels(message.channels, connection.ws).join(' ')
);
message.channels.forEach(channel => {
redisPub.publish(channel, message.message);
});
console.log(`> PUBLISH to: [${message.channels}]`);
console.log(`> PUBLISH 📩: ${message.message}`);
break;
case 'WS_CLOSE':
deleteWSFromChannel(connection.ws);
break;
};
}
catch (err) {
console.error(`👻 x ${connection}: ${err}`)
}
})
server.listen(EXPRESS_PORT, '0.0.0.0', err => {
if (err) {
throw err;
}
console.log(`
> ⚗️ Reactive - Websocket - Redis server is running
> ✌️ Server started on port: ${server.address().port}
`);
})