Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect on Websocket Drop #1558

Closed
Plinpod opened this issue Apr 18, 2018 · 4 comments
Closed

Reconnect on Websocket Drop #1558

Plinpod opened this issue Apr 18, 2018 · 4 comments
Assignees
Labels
Enhancement Includes improvements or optimizations

Comments

@Plinpod
Copy link

Plinpod commented Apr 18, 2018

Hello,

I am making a websocket connection to Infura Ropsten
wss://ropsten.infura.io/ws

and then making contract event subscription like so
contract.events.MyEvent() .on('data', function(event) { console.log('Event Generated', data) }) .on('error', function(error) { console.log('Event Error', error) })

Infuras websockets seem to be unstable and drop randomly. Unfortunately the websocket drops silently in the background and my event never receives the error. My app just continues to work and stops receiving event data.

I can tell that websocket drops by running setInterval on
web3.eth.net.isListening()

Is there a graceful way to catch the websocket drop and reconnect immediately? If not what is a safe interval to run isListening on?

Thanks!

@Dobberoonski75
Copy link

Dobberoonski75 commented Apr 23, 2018

I've been having this issue as well. Is there any sort of keep-alive message we can send to prevent disconnects altogether or an .on('close', (event) => {...}) we can use to detect a dropped connection and open a new one?

EDIT:
After extensive digging into the websocket object, I've found there is an onclose event. It's called like this:

const wes3 = new Web3(new Web3.providers.WebsocketProvider('wss://rinkeby.infura.io/ws'));

wes3._provider.on('end', (eventObj) => {
    console.log('Disconnected: ');
    console.log(eventObj);
});

There is also an on('connect', ...) and an on('error', ...) function as well, both also return event objects.

I still don't know the best way to reconnect the websocket, and it still keeps randomly disconnecting every now and then (the websocket timeout option doesn't work at all) and I don't think there is a 'keep-alive' message thing. But hopefully using the .on('end', ...) helps you catch a socket when it does drop @Plinpod

@cmaliwal
Copy link

I am also facing the same issue.I use websocket (ws) coneection to Infura node

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

sometimes it's works fine or sometimes it give error on console :

Error: CONNECTION ERROR: Couldn't connect to node on WS.

Uncaught (in promise) Error: CONNECTION ERROR: Couldn't connect to node on WS.

@refrigerator
Copy link

Also having this issue. Handling connection in errors using provider.on('end', err => {...}) and provider.on('error', err => {...}) doesnt solve it :(

@nivida nivida added the In Progress Currently being worked on label Aug 9, 2018
@nivida nivida self-assigned this Aug 9, 2018
@ztnark
Copy link

ztnark commented Aug 15, 2018

Does anyone have a working solution via polling web3.eth.net.isListening()?

@nivida nivida added Enhancement Includes improvements or optimizations and removed In Progress Currently being worked on labels Mar 7, 2019
@nivida nivida closed this as completed Mar 7, 2019
@nivida nivida mentioned this issue Nov 21, 2019
7 tasks
@ryanio ryanio mentioned this issue Apr 15, 2020
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Includes improvements or optimizations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants