12

Is there something that I can do on the client side to detect that the socket.io websocket is not available? Something along the lines of:

  • server starts as per usual
  • clients connect
  • messages are sent back and forth between server and client(s)
  • server shuts down (no longer available)
  • warn the connected clients that the server is not available

I tried to add the 'error' and 'connect_failed' options on the client side but without any luck, those didn't trigger at all. Any ideas from anyone how I can achieve this?

3
  • 3
    var sio = io.connect(); sio.socket.on('error', function (reason){ console.error('Unable to connect Socket.IO', reason); }); did you try? Commented May 9, 2013 at 3:58
  • Yes. I did try this - this is on the client side right? Also tried "connect_failed" but no luck. Commented May 9, 2013 at 4:06
  • Anh Tú's solution works when the client tries to connect to a server which is down. So basically one would need both scripts to cover all possible scenarios. Commented Apr 11, 2014 at 16:22

4 Answers 4

12

The disconnect event is what you want to listen on.

var socket = io.connect();

socket.on('connect', function () {
  alert('Socket is connected.');
});

socket.on('disconnect', function () {
  alert('Socket is disconnected.');
});
Sign up to request clarification or add additional context in comments.

1 Comment

I recommend also listening for the connect_error event suggested by cbautista. These two events are useful, but they will display nothing if the initial connection attempt fails.
11

If you want to be able to detect that the client was not able to connect to the server, then try using connect_error. This works for me with socket.io-1.3.5.js. I found this in https://stackoverflow.com/a/28893421/2262092.

Here's my code snippet:

var socket = io.connect('http://<ip>:<port>', {
    reconnection: false
});
socket.on('connect_error', function() {
    console.log('Failed to connect to server');
});

Comments

0

hit this bug during my development and noticed my event calls were doubling up every time i reset the server, as my sockets reconnected. Turns out the solution that worked for me, which is not duping connections is this

var socket = io.connect();

socket.on('connect', function () {

console.log('User connected!');

});

socket.on('message', function(message) {

console.log(message);

});

( Found this at https://github.com/socketio/socket.io/issues/430 by KasperTidemann )

Turns out, it was becuase I put the 'message' listener inside the 'connect' function. Seating it outside of the listener, solves this problem.

Cheers to Kasper Tidemann, whereever you are.

Moving on!!

Comments

0

connect_error didn't work for me (using Apache ProxyPass and returns a 503).

If you need to detect an initial failed connection, you can do this.

var socket;
try {
    socket = io();
}
catch(e) {
    window.location = "nodeServerDown.php";
}

Redirects the user to a custom error page when the server is down.

If you need to handle a disconnect after you've connected once.

You do this:

socket.on('disconnect', function() {
    //whatever your disconnect logic is
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.