[SOLVED] Getting 4004 Draining Connection Error on polygon mumbai websecket => reconnect settings

Iā€™m consistently getting this ā€˜4004 Draining Connectionā€™ error. It happens a few times a day. Iā€™m using the web socket connection and am listening to events like so:

contract.events.Transfer(function(err, event) {
ā€¦
});

Here is how Iā€™m connecting:
let provider = new Web3.providers.WebsocketProvider(ā€œwss://speedy-nodes-nyc.moralis.io/ā€ + MORALIS_ID + ā€œ/polygon/mumbai/wsā€, options);
web3 = new Web3(provider);

Is this due to being rate limited? Please let me know what the fix would be.

Here is the stack trace:
web: Error: CONNECTION ERROR: The connection got closed with the close code 4041 and the following reason string Draining connection
web: at Object.ConnectionError (/var/app/current/node_modules/web3-core-helpers/lib/errors.js:66:23)
web: at Object.ConnectionCloseError (/var/app/current/node_modules/web3-core-helpers/lib/errors.js:53:25)
web: at /var/app/current/node_modules/web3-core-requestmanager/lib/index.js:120:50
web: at Map.forEach ()
web: at WebsocketProvider.disconnect (/var/app/current/node_modules/web3-core-requestmanager/lib/index.js:119:37)
web: at WebsocketProvider.emit (/var/app/current/node_modules/eventemitter3/index.js:181:35)
web: at WebsocketProvider._onClose (/var/app/current/node_modules/web3-providers-ws/lib/index.js:152:10)
web: at W3CWebSocket._dispatchEvent [as dispatchEvent] (/var/app/current/node_modules/yaeti/lib/EventTarget.js:115:12)
web: at W3CWebSocket.onClose (/var/app/current/node_modules/websocket/lib/W3CWebSocket.js:228:10)
web: at WebSocketConnection. (/var/app/current/node_modules/websocket/lib/W3CWebSocket.js:201:17)
web: at WebSocketConnection.emit (events.js:400:28)
web: at WebSocketConnection.emit (domain.js:470:12)
web: at WebSocketConnection.handleSocketClose (/var/app/current/node_modules/websocket/lib/WebSocketConnection.js:389:14)
web: at TLSSocket.emit (events.js:412:35)
web: at TLSSocket.emit (domain.js:470:12)
web: at net.js:675:12 {
web: code: 4040,
web: reason: ā€˜Draining connectionā€™
web: }
web: Error: CONNECTION ERROR: The connection got closed with the close code 4040 and the following reason string Draining connection
web: at Object.ConnectionError (/var/app/current/node_modules/web3-core-helpers/lib/errors.js:66:23)
web: at Object.ConnectionCloseError (/var/app/current/node_modules/web3-core-helpers/lib/errors.js:53:25)
web: at /var/app/current/node_modules/web3-core-requestmanager/lib/index.js:120:50
web: at Map.forEach ()
web: at WebsocketProvider.disconnect (/var/app/current/node_modules/web3-core-requestmanager/lib/index.js:119:37)
web: at WebsocketProvider.emit (/var/app/current/node_modules/eventemitter3/index.js:181:35)
web: at WebsocketProvider._onClose (/var/app/current/node_modules/web3-providers-ws/lib/index.js:152:10)
web: at W3CWebSocket._dispatchEvent [as dispatchEvent] (/var/app/current/node_modules/yaeti/lib/EventTarget.js:115:12)
web: at W3CWebSocket.onClose (/var/app/current/node_modules/websocket/lib/W3CWebSocket.js:228:10)
web: at WebSocketConnection. (/var/app/current/node_modules/websocket/lib/W3CWebSocket.js:201:17)
web: at WebSocketConnection.emit (events.js:400:28)
web: at WebSocketConnection.emit (domain.js:470:12)
web: at WebSocketConnection.handleSocketClose (/var/app/current/node_modules/websocket/lib/WebSocketConnection.js:389:14)
web: at TLSSocket.emit (events.js:412:35)
web: at TLSSocket.emit (domain.js:470:12)
web: at net.js:675:12 {
web: code: 4040,
web: reason: ā€˜Draining connectionā€™
web: }

Hi,
Is this something that started to happen recently, or since when it is this happening?

Itā€™s been happening since I deployed on Polygon mainnet a few days ago.

Just FYI if anyone else is experiencing this, adding the reconnect property to the provider options fixes it.
reconnect: {
auto: true,
delay: 10000,
maxAttempts: 10,
}

2 Likes