Socket.io connect with server offline

javascript, node.js, socket.io

Solution

Use

- `this.socket.on("connect", callback)` to catch connection events

- `this.socket.on("disconnect", callback)` to catch disconnection events

- `this.socket.on("connect_failed", callback)` to catch failed connection attempts

- `this.socket.io.on("connect_error", callback)` to catch if the server is offline

You can find all events, at https://github.com/LearnBoost/socket.io/wiki/Exposed-events

Problem

How do I detect if the server is offline, or for some other reason cannot connect. My code looks something like this. ``` this.socket = io.connect(connectionInfo, { reconnect:false }); ``` It does not throw any error, so a try/catch clause is not working.

Original source