I have a server set up using node.js and I'm trying to connect to the server with socket.io. The server used to work perfectly 2 months ago but for some reason it does not work anymore. The server code looks like the following:
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
var io = require('socket.io')(server);
io.sockets.on('connection', function (socket) {
console.log ('Client connected.'); });
console.log ('Server started.');
server.listen(3000, '0.0.0.0');
On my client side, I'm using the line
socket = io.connect("http://localhost:3000");
to try to connect to my server, but it doesn't work. The connection works perfectly if I use the line
socket = io.connect("http://127.0.0.1:3000");
If I type localhost:3000 on the browser, it sends me to the page saying Hello World as intended, same as 127.0.0.1, so it is not working only when I'm using io.connect.
Things that I have tried are:
Considering what happens from web browser, localhost seems to work but it only does not work when I use socket.io with it. I have been unable to solve this problem that suddenly appeared, so I would appreciate it a lot if someone can give me some insights on what can be causing this.
It seems like an IPv4/IPv6 issue. You may try changing this:
server.listen(3000, '0.0.0.0');
to this:
server.listen(3000, '::1');
or this:
server.listen(3000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With