-
-
Notifications
You must be signed in to change notification settings - Fork 892
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
webSocket auto reconnect doesn't work if socket is closed #2325
Comments
Same issue on backend side (nest.js framework). Moreover, viem doesn't notify about socket disconnect/reconnect by logs, just looks like that websocket is just stuck without any reason. Here is client creation code.
|
I have the same problem, moreover i didn't find a way of being "notified" ( like an eventListener ) about the connection status. |
Same problem for us, impossible to index via websocket, no disconnection errors and no automatic reconnection. This is really annoying, because we're going to have to listen via http and explode our alchemy quotas! |
You can increase the number of reconnect attempts (https://viem.sh/docs/clients/transports/websocket.html), but I don't see a way to be notified of the connection status.. the only way i can think of indexing all blocks with viem would be to go chunk by chunk using http requests. |
in my case, it because the backend nginx timeout setting when I subscribe a topic with indexed topics, it may have long time without response, if backend setting a max timeout, the socket will be closed, return code 1006('Abnormal Closure') there are two way to resolve this issue:
I just implement the keep-alive feature: #2516 it will keep send happy to receive some feedbacks |
One thing I miss/couldn't find is a way to subscribe to connection events, like "connected," "disconnected," or "error," in order to make the application more self-aware. During my initial tests with WS transport, I found that WS would disconnect and fail silently, and my application would stop. i think either i'm missing something from the documentation or there is room for improvement. |
I think you can get the const WS_URL = ''
const webSocketClient = createPublicClient({
chain: mainnet,
transport: webSocket(WS_URL)
})
const socketClient = await getWebSocketRpcClient(WS_URL)
const socket = socketClient.socket |
interesting i didn't find docs for this function on viem website, but my VS CODE finds it. thank you. |
Fixed via 44281e8 |
^ amazing Would be possible to also add documentation on how to listen for connection status on the websocket so services that rely on monitoring each block can recover quickly? From @izayl i got the hint to look for thank you |
Hey @jxom It seems that the problem persists, we updated our staging environment yesterday and this morning the events are not caught... So the websocket connection must have closed... |
@flux0uz u can try with following code to verify the ws close reason first, see what's the code of close event replace the if (publicClient.transport.type === 'webSocket') {
const client = await getWebSocketRpcClient(ws_rpc_url!)
const socket = client.socket
socket.addEventListener('close', (e) => {
console.error('websocket closed', e)
})
} |
Thanks @izayl, no errors were detected on the socket "close" event. I've just realised that the problem is with the fallback([]). By removing the fallback on the public provider used to listen to events, everything works normally! @jxom |
Back to this issue! Another test this morning, listening to the sockets close (with @izayl code). No close log, and we don't receive any events. PublicProvider with a single websocket connection via Alchemy and no fallback. |
double check your ABI, maybe has wrong order or missed |
Nop works correctly with http transport, but uses a ton of our Alchemy quota! |
I'm now listening to |
i confirm. it doesn't work. please run a minimum reproducible for at least 2 hours before merging. |
You might want to take a look at alchemy's ws implementation. tested it and its rock solid with no connection breaking. Looking at your fix why do use rpc endpoints: |
Sorry for a bit of spamming but I really need this feature as it's blocking us from moving forward. This seems to be a common issue for web3 libraries -- ethers having the same issue and no ETA. Web3 js claims to have resolved the issue recently. If you need my help I'll be glad to assist. |
I published a release this morning which makes socket closure handling a bit more robust. If that doesn't solve your issue, please favor opening a new issue with a minimal reproducible example over replying to this closed issue. |
Unfortunately, it still doesn't work. I created another ticket with steps on how to reproduce the issue. |
This issue has been locked since it has been closed for more than 14 days. If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread. |
Check existing issues
Viem Version
2.13.1
Current Behavior
We use
webSocket
transport in our frontend app. After some period of inactivity browsers automatically close the web socket connections. When a user leaves the tab open and gets back after a while,webSocket
transport is unable to recover from the socketCLOSED
state and requests keep failing.Expected Behavior
webSocket
transport creates new connection in place of the closed one.Steps To Reproduce
Minimal reproducible example using only
viem
:In the provided code the second request fails because the web socket connection is closed.
Link to Minimal Reproducible Example
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: