Can't subscribe to infura mainnet using websockets. Ropsten works fine

I have a dapp with a node js server that uses websockets to connect to infura. I have the following line:

web3 = new Web3(new Web3.providers.WebsocketProvider('wss://ropsten.infura.io/ws'))

and it works great. This line however does not:

web3 = new Web3(new Web3.providers.WebsocketProvider('wss://ropsten.infura.io/v3/MYID'))

And neither of the following mainnet lines work:

web3 = new Web3(new Web3.providers.WebsocketProvider('wss://main.infura.io/v3/MYID'))
web3 = new Web3(new Web3.providers.WebsocketProvider('wss://main.infura.io/ws'))

The error I get is simply “connection not open on send()”.

I know that my infura is set up properly because I am able to successfully migrate my project using truffle using the “https://ropsten.infura.io/v3/MYID” endpoint (didn’t try mainnet yet as I don’t want to if I can’t get my node server working).

Update: turns out its mainnet not main. When I change the above to mainnet it works, but only for the non authenticated version. How do I get it to work with the authentication version?

Hi @mcplums this appears to be unusual, to confirm, it is definitely mainnet and not main, can you confirm that you are using a Project ID generated from our current Infura dashboard as MYID in your example? If so, can you provide the email associated with the account so I can diagnose further.

I confirm that I am using mainnet not main and that I am using a project ID generated from the current dashboard. My email address is andrew at stanger dot co dot uk. Thanks in advance buddy

Looks like your URL is incorrect actually, try wss://mainnet.infura.io/ws/v3/MYID

That was the problem!! Thanks :slight_smile:

However I got the url I used from my project page- under keys, for endpoint mainnet, it gives the url without the /ws/ . So this is likely giving the url for http not websockets? I think this could be clearer on the project page.

Very much agree, we are tracking an issue to improve the URL generator.

I faced same problem. But I still connect, after period of time, it get “connection not open on send()”
I used:
Mainnet: wss://mainnet.infura.io/ws/v3/47e0cb60111148b09453d1be50d9782b
Ropsten: wss://ropsten.infura.io/ws/v3/47e0cb60111148b09453d1be50d9782b
What’s wrong with that?
I got it from Infura.

Hi @vanphandinh - welcome to the Infura community! Can you send over your code that you’re using to connect to the WebSockets? And how long does it typically take to see the “connection not open on send()” error?

I used:

const web3 = new Web3(InfuraWebsocketProvider, {
    clientConfig: {
        // Useful to keep a connection alive
        keepalive: true,
        keepaliveInterval: 60000, // ms
    },
    // Enable auto reconnection
    reconnect: {
        auto: true,
        delay: 5000, // ms
        maxAttempts: 5,
        onTimeout: false,
    },
})

After a while, I got my connection was dropped by remote.

Hi @vanphandinh I’m not sure what the keepalive param is doing there but if there are not messages coming through the connection it will close. Have you tried having a newHeads subscription type over that connection, that reliable will keep the connection open.