96

I'm using Socket.io, and I'd like to know the status of connection to the server from the client-side.

Something like this:

socket.status // return true if connected, false otherwise

I need this information to give a visual feedback to the user if the connection has dropped or it has disconnected for any reason.

2
  • 1
    Are you using socket.io client listed: github.com/LearnBoost/socket.io-client or simple socket.io library?
    – jsist
    Commented May 13, 2013 at 9:11
  • I'm using the javascript file served from Node.js, so I think it's the first one. Commented May 13, 2013 at 9:31

6 Answers 6

145

You can check the socket.connected property:

var socket = io.connect();
console.log('check 1', socket.connected);
socket.on('connect', function() {
  console.log('check 2', socket.connected);
});

It's updated dynamically, if the connection is lost it'll be set to false until the client picks up the connection again. So easy to check for with setInterval or something like that.

Another solution would be to catch disconnect events and track the status yourself.

15
  • 3
    It works perfectly. I didn't find this property on the docs, so thanks for the precious info ;) Commented May 13, 2013 at 10:21
  • 7
    I found it by console.log'ing the socket variable ;)
    – robertklep
    Commented May 13, 2013 at 10:23
  • 22
    In v1.0 it's now socket.connected not socket.socket.connected. Commented Aug 17, 2014 at 23:45
  • 2
    In socket.io 2.0.2 I'm using socket.connected and it's behaving as expected (returning either true or false). Note it's false immediately after calling io(server) but becomes true almost immediately after. I'm not sure if that's a race condition, though I note there isn't a callback option to io() so I think it's just how it works. Commented Jun 27, 2017 at 1:49
  • 2
    @IainCollins instead of callbacks, it uses events. io(...) is merely telling socket.io to connect, but only when the connect event has been emitted has it actually connected.
    – robertklep
    Commented Jun 27, 2017 at 5:32
9

You can check whether the connection was lost or not by using this function:-

var socket = io( /**connection**/ );
socket.on('disconnect', function(){
//Your Code Here
});

Hope it will help you.

8

These days, socket.on('connect', ...) is not working for me. I use the below code to check at 1st connecting.

if (socket.connected)
  console.log('socket.io is connected.')

and use this code when reconnected.

socket.on('reconnect', ()=>{
  //Your Code Here
});
3

Track the state of the connection yourself. With a boolean. Set it to false at declaration. Use the various events (connect, disconnect, reconnect, etc.) to reassign the current boolean value. Note: Using undocumented API features (e.g., socket.connected), is not a good idea; the feature could get removed in a subsequent version without the removal being mentioned.

1
2

@robertklep's answer to check socket.connected is correct except for reconnect event, https://socket.io/docs/client-api/#event-reconnect As the document said it is "Fired upon a successful reconnection." but when you check socket.connected then it is false.

Not sure it is a bug or intentional.

0
curl "<the server URL>/socket.io/?EIO=4&transport=polling"

Execute the above curl in command prompt or Postman Output should be look like below for successfull connections

0{"sid":"Lbo5JLzTotvW3g2LAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":20000}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.