From 6966854f3d887e48e542e725c972ce5280a2cc03 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 2 Apr 2017 21:27:14 +0200 Subject: [PATCH] Add heartbeat to websockets streaming API connections --- streaming/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/streaming/index.js b/streaming/index.js index 0f838e411a..7edf6203fb 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -215,8 +215,11 @@ const streamHttpEnd = req => (id, listener) => { // Setup stream output to WebSockets const streamToWs = (req, ws) => { + const heartbeat = setInterval(() => ws.ping(), 15000) + ws.on('close', () => { log.verbose(req.requestId, `Ending stream for ${req.accountId}`) + clearInterval(heartbeat) }) return (event, payload) => { @@ -234,6 +237,10 @@ const streamWsEnd = ws => (id, listener) => { ws.on('close', () => { unsubscribe(id, listener) }) + + ws.on('error', e => { + unsubscribe(id, listener) + }) } app.use(setRequestId)