From 0075432a656de2042c5da60f9b5a5027af57f942 Mon Sep 17 00:00:00 2001 From: gerardo15 Date: Sun, 4 Jul 2021 17:43:22 +0200 Subject: [PATCH 1/2] add config.keepaliveForce Why? If you have ever worked with web3 subscriptions, in particular with infura's node, you will stop receiving messages after a few minutes of inactivity. When a subscription its established for example a subscription to logs, there would be only incomming messages from the node. The package currently doesn't ping if messages are being received. I propose adding this option keepaliveForce, that would force the keepalive to be sent Here you can see workarounds that need to be done, but could be solved if this option its enabled https://medium.com/@pauloostenrijk/infura-drops-your-websocket-connections-after-a-while-and-here-is-a-fix-for-it-3413ca8253b --- lib/WebSocketConnection.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/WebSocketConnection.js b/lib/WebSocketConnection.js index 219de631..6b3e274a 100644 --- a/lib/WebSocketConnection.js +++ b/lib/WebSocketConnection.js @@ -262,7 +262,9 @@ WebSocketConnection.prototype.handleGracePeriodTimer = function() { WebSocketConnection.prototype.handleSocketData = function(data) { this._debug('handleSocketData'); // Reset the keepalive timer when receiving data of any kind. - this.setKeepaliveTimer(); + if (!this.config.keepaliveForce) { + this.setKeepaliveTimer(); + } // Add received data to our bufferList, which efficiently holds received // data chunks in a linked list of Buffer objects. From 9ed7fef4d08618dc521722a9ac7d1c7134391e27 Mon Sep 17 00:00:00 2001 From: gerardo15 Date: Thu, 8 Jul 2021 12:07:17 +0200 Subject: [PATCH 2/2] Add to docs --- docs/WebSocketServer.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/WebSocketServer.md b/docs/WebSocketServer.md index e0ea60c6..77f9eb66 100644 --- a/docs/WebSocketServer.md +++ b/docs/WebSocketServer.md @@ -58,6 +58,9 @@ If true, the server will automatically send a ping to all clients every `keepali **keepaliveInterval** - uint - *Default: 20000* The interval in milliseconds to send keepalive pings to connected clients. +**keepaliveForce** - boolean - *Default: false* +if true, the server will not reset the keepalive timer when any data is received from the client, forcing to send the ping on the Interval. + **dropConnectionOnKeepaliveTimeout** - boolean - *Default: true* If true, the server will consider any connection that has not received any data within the amount of time specified by `keepaliveGracePeriod` after a keepalive ping has been sent. Ignored if `keepalive` is false.