Get connection status on Socket.io client
Asked Answered
S

6

94

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.

Slowmoving answered 13/5, 2013 at 8:55 Comment(2)
Are you using socket.io client listed: github.com/LearnBoost/socket.io-client or simple socket.io library?Dna
I'm using the javascript file served from Node.js, so I think it's the first one.Slowmoving
B
143

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.

Biped answered 13/5, 2013 at 9:40 Comment(15)
It works perfectly. I didn't find this property on the docs, so thanks for the precious info ;)Slowmoving
I found it by console.log'ing the socket variable ;)Biped
In v1.0 it's now socket.connected not socket.socket.connected.Br
@Biped But its returning true if my client gets soft killed but connection is disconnected from server. And false if I kill server.Rasure
Seams like now in v1.0 version it is io.socket.isConnected()Speculative
Is there anyway I can find documentation on javascript objects for libraries or do we just have to go in bind every-single-time?Jerryjerrybuild
how to save ourself unauthorised from connect to our socket.io connection??Hanoverian
@VikasBansal please create a new question for that, it's not related to this one.Biped
can u help me bcoz i am getting some problem to check socket s connected or notAnkus
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.Witham
@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.Biped
@Biped Sorry I just re-read your answer instead of skimming it and realised that exactly what you said.Witham
@Biped do you know what I describe below (my answer)?Undertenant
@Undertenant not sure why socket.connected wouldn't be set after a reconnect, you'd expect it to be true.Biped
In 'ng-socket-io' is .socket.ioSocket.connected;Teodora
Q
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.

Quickel answered 27/1, 2017 at 14:9 Comment(0)
V
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
});
Vaenfila answered 29/1, 2019 at 9:30 Comment(0)
P
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.

Parasol answered 18/3, 2018 at 17:27 Comment(1)
socket.connected is part of the documentation and can be referenced here: socket.io/docs/client-api/#socket-connectedMudslinging
U
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.

Undertenant answered 7/6, 2018 at 4:30 Comment(0)
W
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}
Workable answered 23/1 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.