M.Dagiya
M.Dagiya

Reputation: 203

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);

Upvotes: 0

Views: 273

Answers (1)

robertklep
robertklep

Reputation: 203231

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!');
  ...
});

Upvotes: 3

Related Questions