-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
38 lines (30 loc) · 958 Bytes
/
index.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
function sse(req, res, next) {
req.socket.setKeepAlive(true);
req.socket.setTimeout(0);
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.status(200);
// export a function to send server-side-events
res.sse = function sse(string) {
res.write(string);
// support running within the compression middleware
if (res.flush && string.match(/\n\n$/)) {
res.flush();
}
};
// write 2kB of padding (for IE) and a reconnection timeout
// then use res.sse to send to the client
res.write(':' + Array(2049).join(' ') + '\n');
res.sse('retry: 2000\n\n');
// keep the connection open by sending a comment
var keepAlive = setInterval(function() {
res.sse(':keep-alive\n\n');
}, 20000);
// cleanup on close
res.on('close', function close() {
clearInterval(keepAlive);
});
next();
}
module.exports = sse;