]> cat aescling's git repositories - mastodon.git/commitdiff
Add heartbeat to websockets streaming API connections
authorEugen Rochko <eugen@zeonfederated.com>
Sun, 2 Apr 2017 19:27:14 +0000 (21:27 +0200)
committerEugen Rochko <eugen@zeonfederated.com>
Sun, 2 Apr 2017 19:27:14 +0000 (21:27 +0200)
streaming/index.js

index 0f838e411a16c3d49d74f848977be556fdb0015e..7edf6203fb0632876d9dd10feb2c914a9766f447 100644 (file)
@@ -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)