Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 74aef21

Browse files
committed
Try to reconnect on timeout too
In the case any websocket call takes too long to return and a timeout was set for the provider to timeout, the provider should try to restart the connection. This could happen, for instance, if the client loses connection with the server, the server closes the connection and later, the connectivity is up again but since the client did not receive the closing frame *and* the client does not attempt to send any package to the server, no error is observed. `websocket` implementation for Node.js has an option to send keep-alive frames and detect such scenarios, but the standard browser W3C WebSocket does not, so it is "vulnerable" to this kind of failure which will mostly affect web3 subscriptions.
1 parent 2ba71bd commit 74aef21

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

packages/web3-providers-ws/src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ WebsocketProvider.prototype._addResponseCallback = function(payload, callback) {
244244
setTimeout(function () {
245245
if (_this.responseCallbacks[id]) {
246246
_this.responseCallbacks[id](errors.ConnectionTimeout(_this._customTimeout));
247+
247248
delete _this.responseCallbacks[id];
249+
250+
// try to reconnect
251+
if (_this.connection.reconnect) {
252+
_this.connection.reconnect();
253+
}
248254
}
249255
}, this._customTimeout);
250256
}

0 commit comments

Comments
 (0)