How to keep-alive a WebSocket? #1937
Replies: 14 comments
-
any traction on this? |
Beta Was this translation helpful? Give feedback.
-
Sorry, I haven't had a chance to look into this yet, I have a few other admin things I want to get to first. Is the problem it disconnects after a short time? You may be able to use |
Beta Was this translation helpful? Give feedback.
-
thanks for the response. Do you important stuff, just been going back and forth with infura trying to track down why one service loses connection, and the other doesn't. Both are connecting the same with ethers via websocket. My first attempt at a fix was the above, I too thought that might work. Infura recommends adding a keep-alive ping. I am still not sure what the issue here is, but I was curious if there was a comparable options. thanks |
Beta Was this translation helpful? Give feedback.
-
My gas-ticker stays connected for hours at a time, but I do use that |
Beta Was this translation helpful? Give feedback.
-
I will look into this, thank you. And I do need to write some reconciliation script to re-connect if it gets severed. I'll take a look, thanks again. |
Beta Was this translation helpful? Give feedback.
-
@tobyjaguar you can use the web3-providers-ws package and pass it in as a Web3Provider to ethers |
Beta Was this translation helpful? Give feedback.
-
Interesting. I ended up writing a heartbeat function into my process as there are many reconnects with infura. But this looks promising as a more permanent solution. Would need to resubscribe to events on reconnect. I will look into it. Thank you. |
Beta Was this translation helpful? Give feedback.
-
yeah you can probably submit a pr to the web3-providers-ws package that auto subscribes on reconnect! |
Beta Was this translation helpful? Give feedback.
-
@tobyjaguar can you share your heartbeat function? I'm on the same boat, looking to re-connect ethers.js web socket in case the connection is dropped. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I never explored @rymnc 's suggestion regarding the options of the web3-providers-ws package:
That is probably a better way to go, but I had already attempted a heartbeat process. After tinkering with it for a couple weeks this seems to work for my purposes. It is probably a bit overkill, but I grab a beat tolerance from config, register a timestamp and then just continuously consume the heartbeat function().
The above has kept my processes online since I implemented the above. Things go horribly wrong if the process loses the internet connection, but it can heal a lost connection with infura, which seems to happen all the time. |
Beta Was this translation helpful? Give feedback.
-
I think it would be best if someone extended the websocket provider/made a pr on the existing one which handles websocket reconnections + resubscriptions etc! |
Beta Was this translation helpful? Give feedback.
-
alright, my band-aid solution doesn't work so well on L2 as those blocks are inconsistent. This is how I am currently connecting to L2, until infura catches up. |
Beta Was this translation helpful? Give feedback.
-
@smartins don't know if you ever got a keepalive function going, but I have had success with this design: thanks to @mikevercoelen for posting |
Beta Was this translation helpful? Give feedback.
-
@tobyjaguar I also face this kind of problem, Can you post your code |
Beta Was this translation helpful? Give feedback.
-
Infura is telling me that I should ping the connection to keep it alive for websockets. They suggested using this options in web3:
https://web3js.readthedocs.io/en/v1.2.11/web3.html#providers
Is there comparable options in ethers that I can take advantage of?
Beta Was this translation helpful? Give feedback.
All reactions