M.Dagiya M.Dagiya - 1 year ago 113
Node.js Question

socket.io-client web socket connection failing in node.js child process

I am using simple socket.io client module to connect to web socket but the connection is failing. The way I have learned is that right after you define socket, you can access connected property to find out the status of connection and it always return false. I am trying to connect to web socket in a child process on the same server where my main process is running.

var socket = require('socket.io-client')("ws://xx.xx.xxx.xxx");
console.log(socket.connected);

Answer Source

SocketIO connections should be initiated over HTTP(S):

var socket = require('socket.io-client')('http://localhost:6001');

(instead of ws://...)

Then wait for the connect event; when that fires, the client is connected to the server;

socket.on('connect', function() {
  console.log('connected to server!');
  ...
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download