5

I'm currently using einaros' NodeJS WS module to create a websocket client. When initializing the socket client, I use the 'open' event to determine when the connection is open, but once it's open, how would I be able to check if it's still open?

Is there a property on the 'WebSocket' object that I can use to quickly check the status of the socket?

Is - websocketInstance.readyState - adequate?

Charlie
  • 4,197
  • 5
  • 42
  • 59
  • 1
    `if (socket.readyState === 1)` then the system thinks the socket is still open. The only better assurance you can get is to send a message and await the response. – jfriend00 Dec 02 '14 at 18:47
  • You can also monitor the onclose event if you want to know exactly when the readyState changes to closed. – jfriend00 Dec 02 '14 at 19:02
  • @jfriend00 sometimes the `onclose` [is never fired](http://stackoverflow.com/questions/25352111/nodejs-einaros-ws-connection-timeout) – ᴍᴇʜᴏᴠ Mar 29 '16 at 23:26
  • @TheSexiestManinJamaica - Read the last paragraph in my answer below. – jfriend00 Mar 29 '16 at 23:27

2 Answers2

13

socket.readyState will tell you what the socket thinks its state is. There is no other, more accurate property that you can consult than socket.readyState.

If what you want to know is when the socket has finished connecting and is now considered open, then you can register an event handler for the open event. This will tell you when it has finished connecting.

If you want a real-time notification when the socket closes or experiences an error, you can use an event listener for the error or close events. The close event just means that socket is closing and the socket.readyState value is now changing to closed.

A more guaranteed to be up-to-date method of checking the socket is to first check socket.readyState and, if that says the socket is open, then you can send a test packet to the other end and only when you get a response back from the test packet will you truly know that socket is operational.

The socket.io library (built on top of webSockets) does exactly this, sending regular keep-alive packets and listening for responses. If it goes some period of time with no response, then it will close the existing socket and attempt to re-establish a new connection (figuring the prior connection was lost somehow). In this way, the socket.io library can even survive a server reboot or a temporary drop in client internet connectivity as it will see that the connection was lost and then transparently re-establish a new connection in its place.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    This is a little too abstract for me. I'd appreciate it if you could show me some actual nodejs code. So suppose I want to see if there is a listener on localhost at port 5858. How would I write a function that returns true if one is there and false if not? Thanks. – rocky Apr 25 '15 at 00:26
  • @rocky - that's a different question than what was asked here. I'd suggest you start a new question of your own that asks about your specific situation. You can link the new question here in a comment and I'll take a look after you post it. – jfriend00 Apr 25 '15 at 00:57
1

I think you should watch for the error and disconnect callbacks. That way you'll get notified when it closes.

Max
  • 8,671
  • 4
  • 33
  • 46