-
-
Notifications
You must be signed in to change notification settings - Fork 243
Description
Here is my keep-alive function.
var ka = setInterval(function() {
res.write(": keep-alive\n");
res.flush();
}, 15000);
res.on('close', function() {
clearInterval(ka);
});Used on top of a HTTP response that delivers a stream of JSON updates, formatted as text/event-stream (hence the flush).
The keep-alive is useful for SSE streams that only update once in a while, such as transmitting new data points on graphs that have only one data point per minute.
Usually there is a (network) router or two among the hops between client and server, that has a TCP state table with a timeout of 30 seconds, so the above helps to keep the channel open until the next event is ready for the client.
The expressjs/compression module, when enabled, has this side effect:
(node) warning: possible EventEmitter memory leak detected. 11 drain listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at Gzip.addListener (events.js:239:17)
at Gzip.Readable.on (_stream_readable.js:665:33)
at Gzip.once (events.js:265:8)
at Gzip.Zlib.flush (zlib.js:448:10)
at ServerResponse.flush (compression/index.js:70:16)
at null._repeat (sample.js:2:8)
at wrapper [as _onTimeout] (timers.js:264:19)
at Timer.listOnTimeout (timers.js:92:15)
I'm not adding any event listeners, however. Just flushing the output stream after writing.
Hmm, thinking about it, the keep-alive function is irrelevant. When using text/event-stream, the first big chunk of data will usually be pushed with just a single flush at the end, but once the stream gets "up to date" or whatever, every complete JSON object written will be followed by a flush().
Probably just random chance that this happened to me inside the keep-alive function...
Anyway, the error disappears if the compression module is disabled.