Websocket connection closed in less than a minute

Hello,

I run my script on node.js utlising ethers.js, which connects to BSC speedy node.

I am currently just testing but can’t go any further as the websocket close too frequently.

Following is the snippet where i fetch pending txns. For now I am just testing this on BSC.

//Initialize providers and stuff 

async function run() {
    let bsc = new ethers.providers.WebSocketProvider(provs.BscMorWss);
    bsc.on("pending", (tx) => { 
        bsc.getTransaction(tx)
        .then(function (transaction) {
            if (transaction) {
                console.log("Tx hash:",transaction.hash,"at block ",transaction.blockNumber);
            } else {
                //console.log(tx,'is NULL');//ignore theese
            }
        })
        .catch(function (error) {
            //console.log("Error hash:",error.value.hash,"at block ", error.value.blockNumber);
            console.log(error);
        });
    });

// … so on and so forth and a run() to execute

I believe this is caused by rate limits. I have no idea how that is implemented but with 155 million free request per month I guess it should not have hit the rate limits so soon?

Any help appreciated.

cheers,
George

1 Like

Hi @George

Subscription to pending transactions. Will be counted only as one request. But if you will use getTransaction for each fetched transaction, the request limit will be exhausted very quickly. Because there are a lot of transactions per minute. And it is a very bad idea to process them separately.

I suggest you to rewrite the code to get transaction information periodically. In order to process a huge number of transactions at one time, you can use the Web 3 BatchRequest.

Or you can choose a plan that will support such a large number of requests per minute

Happy BUIDLing :man_mechanic:

" if you will use getTransaction for each fetched transaction, the request limit will be exhausted very quickly.

I didn’t know that. I shouldn’t have executed getTransaction on every single tx, I did so to test. Its not practical for real app anyway.

Thank you!

cheers,
George

1 Like

It works. But the connection will close in less than a minute, as you already know :sweat_smile:

Yeah, got ya! Will do things that are more practical from now on. :joy:

1 Like