How do i know if connection is alive with websockets?
Asked Answered
E

2

24

I have a webapp, which is running in a browser. That webapp is connected to a server, which uses websockets. So the communication between the server and my client/browser is based on websockets. If some magic event occurs on the server, some webservice sends a new XML / JSON to my webapp and the new data gets displayed.

But how do i, as the client / browser, know if the connection is stil alive? Lets say i do not get any new XML for about 30 seconds. How would i know if the connection is closed/broken/server offline or everything is fine, but on the server himself no new magic event occured.

Enchiridion answered 4/8, 2012 at 14:8 Comment(0)
E
20

3 ways:

  • rely on TCP to detect loss of connectivity, which will ultimately pop up in JS onclose event
  • send WebSocket pings from server .. browsers will reply with WS pongs, loss of connectivity is probably more robustly detected also on client side
  • send app level heartbeats from browser to server, server need to have logic to reply. you can't trigger WS pings from browsers (in JS)
Efrem answered 4/8, 2012 at 14:43 Comment(4)
Is there nothing defined in the standard? or do i have to do all the "work" manually?Enchiridion
The standard provides a way to detect and notify endpoints of TCP related connection failures (which tends to include many cases of server issues). If you want to be able to detect failures before TCP figures it out or error conditions where the TCP connection is alive but the server itself is not processing messages you will need to do it manually.Wheelwork
ok i understand it, but it seems a bit vague. It looks like 7 different roads to rome.Enchiridion
You can attempt to write on the socket at regular intervals. If the socket is broken an error is thrown you can catch that error and re-establish the socket then.Certified
S
26

A websocket connection object has a readyState field which will tell you if the connection is still active (from the dart documentation). The readyState can be either

0 - connection not yet established
1 - conncetion established
2 - in closing handshake
3 - connection closed or could not open

You can also define an event handler for the websocket close event if this is something you'd like to handle (try to reconnect, etc).

Seismography answered 17/9, 2012 at 19:47 Comment(5)
For anyone looking at the comment above: In my experience, closing the socket with socket.close() will change the ready stateCaspian
Yes, I agree with Brandon, when you close the connection it shows "3"Acyclic
@megawac is right - I just unplugged my server and the websocket from the browser remains in readyState 1. You have to do a regular ping/pong to determine if the connection remains active.Macnair
Yes closing the connection with ws.close will change the readyState - going offline will not!Nan
@GregJohnson there goes the whole protocol into ping pong modeFabricant
E
20

3 ways:

  • rely on TCP to detect loss of connectivity, which will ultimately pop up in JS onclose event
  • send WebSocket pings from server .. browsers will reply with WS pongs, loss of connectivity is probably more robustly detected also on client side
  • send app level heartbeats from browser to server, server need to have logic to reply. you can't trigger WS pings from browsers (in JS)
Efrem answered 4/8, 2012 at 14:43 Comment(4)
Is there nothing defined in the standard? or do i have to do all the "work" manually?Enchiridion
The standard provides a way to detect and notify endpoints of TCP related connection failures (which tends to include many cases of server issues). If you want to be able to detect failures before TCP figures it out or error conditions where the TCP connection is alive but the server itself is not processing messages you will need to do it manually.Wheelwork
ok i understand it, but it seems a bit vague. It looks like 7 different roads to rome.Enchiridion
You can attempt to write on the socket at regular intervals. If the socket is broken an error is thrown you can catch that error and re-establish the socket then.Certified

© 2022 - 2024 — McMap. All rights reserved.