Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
exilonX opened this issue Mar 17, 2015 · 10 comments
Closed

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

exilonX opened this issue Mar 17, 2015 · 10 comments

Comments

@exilonX
Copy link

exilonX commented Mar 17, 2015

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

@pawanvivekananda
Copy link

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

@nkzawa nkzawa closed this as completed Jan 16, 2016
@swapnaleedhavan
Copy link

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

@ghost
Copy link

ghost commented Jan 22, 2017

Same issue. Doest work for me also...

@rspl1006
Copy link

I have same issue, didn't worked

@TanYongTai
Copy link

I changed from localhost to 127.0.0.1 and it worked.

@lakinmohapatra
Copy link

I changed from localhost to 127.0.0.1 and it worked.

It did not work for me .

@darrachequesne
Copy link
Member

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.

@muneeb706
Copy link

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.

Thanks for this suggestion, it worked for me

@harishkoninti
Copy link

It worked for me

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

@malopa
Copy link

malopa commented Oct 28, 2023

none worked for me same error

this is my conection
app = express()
app.use(cors())

const server = require('http').createServer(app);
const io = require('socket.io')(server,{
cors: {
origin: "*"
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests