Fábio Santos
Fábio Santos

Reputation: 4115

How can I detect whether a socket.io server is connected?

I am developing an app using socket.io and appjs. I am having trouble giving a notice to the user when he causes EADDRINUSE by starting two instances of the app, which he is not supposed to.

Socket.io does detect EADDRINUSE, but I only receive a warning written to the console (not sure if stdout or stderr). I wanted an exception, a callback where I have an err parameter, or at the very least a flag to detect whether the connection is OK.

Just to clarify, I am trying to do this from the server side. In the client side I already have a timeout for client-server connections.

How can I do this?

Upvotes: 1

Views: 1227

Answers (1)

generalhenry
generalhenry

Reputation: 17319

EADDRINUSE is an error emitted by the net modules. Docs here: http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback

In short you need to provide a callback to .on('error' that will handle the error.

server.on('error', function (e) {
  if (e.code == 'EADDRINUSE') {
    console.log('Address in use');
  }
});

Upvotes: 2

Related Questions