about summary refs log tree commit diff
path: root/streaming
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-04-02 21:27:14 +0200
committerEugen Rochko <eugen@zeonfederated.com>2017-04-02 21:27:14 +0200
commit3618cc04ffc33e434b18ef6cee2a5ef79273bf7f (patch)
tree6146ba17c64246a9212c6de8e078bd8ea3d2e7ec /streaming
parent2d07cb57714b7aae96593c8ff293c1c3010a13f9 (diff)
Add heartbeat to websockets streaming API connections
Diffstat (limited to 'streaming')
-rw-r--r--streaming/index.js7
1 files changed, 7 insertions, 0 deletions
diff --git a/streaming/index.js b/streaming/index.js
index 0f838e411..7edf6203f 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)