[SOLVED] CONNECTION ERROR with web3 with websockets and HDWalletProvider => increasing the polling time

Hi to all, i get this error normally and i dont understand what i need to do.

I´m using moralis socket url to connect web3 and HDWalletProvider. The reason is that i need to comunicate to smart contract in bsc and i want to do with an existing wallet.

This is the code from conecting :

const provider = new Web3.providers.WebsocketProvider(urlMoralisSocket, options);

:arrow_down: This is a hack for listen events with HDProvider (I’m getted from here and working https://ethereum.stackexchange.com/questions/103301/the-current-provider-doesnt-support-subscriptions-hdwalletprovider-on-polygon).

HDWalletProvider.prototype.on = provider.on.bind(provider);
let providerHD = new HDWalletProvider(mnemonic, provider);

And i use web3 to instanciate contract and interact with him. Using events, call or send methods.

But i get this error:

Error: PollingBlockTracker - encountered an error while attempting to update latest block:
Error: CONNECTION ERROR: Provider started to reconnect before the response got received!

I’m have options in WebsocketProvider using delay on reconnect. This is options value:

timeout: 50000,
clientConfig: {
keepalive: true,
keepaliveInterval: 60000,
maxReceivedFrameSize: 2000000, // bytes - default: 1MiB, current: 2MiB
maxReceivedMessageSize: 10000000, // bytes - default: 8MiB, current: 10Mib
reconnect: {
auto: true,
delay: 60000,
onTimeout: true,
maxAttempts: 10,

But if i remove (options) i get this error:

Error: CONNECTION ERROR: The connection got closed with the close code 4040 and the following reason string Draining connection

code: 4040,
reason: ‘Draining connection’


Error: PollingBlockTracker - encountered an error while attempting to update latest block:
Error: CONNECTION ERROR: Couldn’t connect to node on WS.

Thanks and i hope that anyone can help me :slight_smile:

if you try without web socket then it works to use send methods?

I need the events to be able to use the sends. Since I am listening to auctions created from a contract and bidding.
Also using sockets is supposed to increase speed right? I am interested in fast send processing.

I was thinking that maybe you can use two systems if that would work separately, one that can send transactions and one that does the other processing

I have already tested it. I had created two web3 one with socket(events) and one with http(send) and the one using socket gives that error and the program terminates. I can’t even handle the error and I have to keep an eye on starting it :upside_down_face:

I was thinking of paying for this service, but it doesn’t work for me. I am now using the quicknode url and it works perfectly.

on what network/chain you had that problem?

I have this problem in binance smart chain. But its only with moralis :expressionless:

Exact same issue here. Also with Moralis Speedy Node Websocket connection. Any solution so far?

Im having the same issue aswell.
This is the error

websockets.exceptions.ConnectionClosedError: code = 4040 (private use), reason = Draining connection

and I keep getting similiar errors, nomatter what websocket provider I use, so this is not a moralis issue.
So far I got the isues in javascript with moralis and different providers and I also get similiar errors with python moralis and also different providers.

The error discription varies from provider to provider but the main thing is, at some point it discconnects.

So ee need a solution to check in our script if there is a valid connection than contiune, some loop or something, with a ping or I dont know.
I really didnt find a good solution on stackoverflw either , so now im stuck, with a disconnecting connection every hour, 2 hours, 10 hours

I solved it by increasing the polling time. Try adding this to your hdwalletProvider:


1 Like