Socket.io-client: Node js - Socket.io-client is not connecting to socket.io server

Created on 17 Mar 2015  路  7Comments  路  Source: socketio/socket.io-client

I am trying to connect to a socket.io-client using the following code:

Server:

// Load requirements
var http = require('http'),
io = require('socket.io');

// Create server & socket
var server = http.createServer(function(req, res){

// Send HTML headers and message
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Aw, snap! 404</h1>');

});
server.listen(8080);
io = io.listen(server);

// Add a connect listener
io.sockets.on('connection', function(socket) {

console.log('Client connected.');

// Disconnect listener
socket.on('disconnect', function() {
    console.log('Client disconnected.');
});

});

Client:

console.log('1');

// Connect to server
var io = require('socket.io-client')
var socket = io.connect('localhost:8080', {reconnect: true});

console.log('2');

// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});

console.log('3');

I don't get the Connected console log or Client Connected console log and I don't know why! The code sample is taken from another question posted: Link and I don't see any solution to the problem...

http://stackoverflow.com/questions/29108594/node-js-socket-io-client-is-not-connecting-to-socket-io-server

Most helpful comment

I changed from localhost to 127.0.0.1 and it worked.

All 7 comments

// Connect to server
var io = require('socket.io-client')
var socket = io.connect('localhost:8080', {reconnect: true});

Use like This

// Connect to server
var io = require('socket.io-client')
var socket = io.connect('http://localhost:8080', {reconnect: true});

I have the same issue.After adding the below lines also issue connection issue not solved.

var io = require('socket.io-client')
var socket = io.connect('http://localhost:8080', {reconnect: true});

Same issue. Doest work for me also...

I have same issue, didn't worked

I changed from localhost to 127.0.0.1 and it worked.

I changed from localhost to 127.0.0.1 and it worked.

It did not work for me .

For future readers, you need to explicitly specify the protocol (as the Node.js process has no window.location object it could deduce the protocol from, which is what is done in the browser here).

// does not work
io.connect("localhost:8080");
// does work
io.connect("http://localhost:8080");

That being said, I think the client should throw an error instead of silently failing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

patrickbussmann picture patrickbussmann  路  6Comments

newpanjing picture newpanjing  路  3Comments

BorntraegerMarc picture BorntraegerMarc  路  4Comments

david-fong picture david-fong  路  4Comments

hyperknot picture hyperknot  路  6Comments