0

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);
7
  • socket.io-client isn't a Websocket client. Also, socket.connected will only be true after the connect event has fired. Commented Jul 27, 2017 at 14:36
  • I have already tried checking the connect event and it never fires. Commented Jul 27, 2017 at 14:42
  • Well, like I said: socket.io-client isn't a Websocket client, it's a SocketIO client and thus requires a SocketIO server to connect to. Commented Jul 27, 2017 at 14:43
  • Sorry, I forgot to mention, I already have a server setup like so,var express = require('express'); var app = express(); var server = app.listen(6001); var io = require('socket.io').listen(server); Commented Jul 27, 2017 at 14:46
  • Try using require('socket.io-client')('http://localhost:6001') (not ws://...) If that still doesn't work, try adding some error handlers. Also, how did you start the child process? Commented Jul 27, 2017 at 14:47

1 Answer 1

3

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!');
  ...
});
Sign up to request clarification or add additional context in comments.

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.