-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebsocket-api.js
206 lines (183 loc) · 4.17 KB
/
websocket-api.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Dependencies
*/
const debug = require('debug')('toppsaker-websocket-api:app');
const highland = require('highland');
const _ = require('lodash-fp');
const EventEmitter = require('events').EventEmitter;
const { Receiver } = require('interprocess-push-stream');
const app = require('express')();
const cors = require('cors');
const http = require('http').Server(app);
const io = require('socket.io')(http, { transports: ['websocket'] });
/**
* Application-specific modules
*/
const config = require('./config');
const inspect = require('./helpers/inspect').bind(null, debug);
/**
* Create streams for the channels
* on which we want to
* listen for data
*/
const newChannel = Receiver({
channel: 'articles:new',
prefix: config.get('database.redis.prefix'),
url: config.get('database.redis.url')
});
const updateChannel = Receiver({
channel: 'articles:update',
prefix: config.get('database.redis.prefix'),
url: config.get('database.redis.url')
});
/**
* Create a new event-emitter
* which we are going to use
* for errors
*
* We'll also make a curryed
* version of eventEmitter.emit
* that we'll use in our
* application
*/
const eventEmitter = new EventEmitter();
eventEmitter.setMaxListeners(1000);
const emit = _.curryN(2, eventEmitter.emit.bind(eventEmitter));
/**
* Create a stream
* where we'll
* collect all the
* errors emitted
* throughout the
* the stream pipeline(s)
*/
const errorStream = highland('error', eventEmitter);
/**
* Create a stream
* with the newChannel
* as the source
*/
const newStream = highland(newChannel)
.compact()
.flatten()
.errors(emit('error'))
/**
* Create a stream
* for monitoring
* refreshes
* (to force frontend updates on user)
*/
const updateStream = highland(updateChannel)
.compact()
.flatten()
.errors(emit('error'))
/**
* Log all the updated
* articles and the
* resulting entries in
* mongodb
*/
newStream
.fork()
.doto(inspect('collectify-new'))
.doto(emit('new'))
.resume()
/**
* Log all the updated
* articles and the
* resulting entries in
* mongodb
*/
updateStream
.fork()
.doto(inspect('collectify-update'))
.doto(emit('update'))
.resume()
/**
* Pipe all errors
* to the error channel
*/
errorStream
.doto(inspect('error-stream'))
.resume()
/**
* Create a route checking
* the availability of the app
*/
app.get('/', (req, res) => {
res.send('Websocket API up!');
});
/**
* Create a socket.io server
* where we broadcast the updates
* to clients
*/
io.on('connection', (socket) => {
debug('a user connected');
/**
* The list of subscriptions for the user
* (in memory)
*/
let subscriptionList = [];
/**
* Listen for client subscriptions
* to specific sources
*/
socket.on('subscribe', (subscriptions) => {
if (Array.isArray(subscriptions)) {
subscriptionList = subscriptions;
}
});
/**
* Create an event handler
* for receiving updates
* (named - so we can handle disconnects)
*/
let newReceiver = (data) => {
if (!_.contains(data._source, subscriptionList)) {
return debug('got new article - but user is not subscribing to it');
}
debug('emitting new article', data);
socket.emit('new', data);
};
/**
* Create an event handler
* for receiving updates
* (named - so we can handle disconnects)
*/
let updateReceiver = (data) => {
if (!_.contains(data._source, subscriptionList)) {
return debug('got updated article - but user is not subscribing to it');
}
debug('emitting updated article', data);
socket.emit('update', data);
};
/**
* Attach the event handlers
* to events
*/
eventEmitter.on('new', newReceiver);
eventEmitter.on('update', updateReceiver)
/**
* Handle socket.io disconnect
* and clean up
*/
socket.on('disconnect', () => {
debug('user disconnected');
/**
* Remove event listeners
* (to avoid memory leaks)
*/
eventEmitter.removeListener('new', newReceiver);
eventEmitter.removeListener('update', updateReceiver);
/**
* Avoid potential memory leak
* by 'nulling' out the array
*/
subscriptionList = null;
});
});
/**
* Attach server to port
*/
http.listen(3002);