Unanswered question

How can I detect a websocket disconnection?

I have a script that periodically sends websocket messages using Websocket Request. Is there any way to detect (and handle) the case when the websocket is disconnected, either by the server or for any other reason?

Theodore T.
Theodore T.

Theodore T.

Level
0
22 / 100
points

Answers

Nouredine A.
Nouredine A.

Nouredine A.

Level
4
5000 / 5000
points
Team

There's no built-in mechanism to detect that a WebSocket channel has been closed before trying to use it sending a WebSocket request.

Obviously you should check why the connection is closed when it should not but  maybe you could put your WebSocket requests inside a try...cache condition and in case of error when sending the request you could try a new connection of the WebSocket channel in the cache condition and try again the request right after

It's feasible if you do not have too many requests to handle like this.

Theodore T.
Theodore T.

Theodore T.

Level
0
22 / 100
points

Thanks for the answer, I think this will work in my case.

Just one clarification. When you say "try a new connection of the WebSocket channel", you mean add a copy of the websocket channel in the catch condition?

Or is there a way to re-use a single websocket channel element, by opening and closing it's connection to the server at will?

Nouredine A.
Nouredine A.

Nouredine A.

Level
4
5000 / 5000
points
Team

Yes i meant to add a copy of your WebSocket channel. If for any reason the connection has been closed we can't reuse the same so it will be a new connection. But again it should not happen and you may have to figure out the root cause of this disconnection.