DEV Community

Doaa Mahely
Doaa Mahely

Posted on

Can't connect to remote server running Socket.io

UPDATE:

It turns out the port 3000 on the remote server wasn't open. Thanks everyone for reading.

=========

Hello everyone.

I have a socket.io client that I connect to the socket.io server I'm running in my localhost using

const io = require('socket.io-client');
const chatSocket = io.connect('http://127.0.0.1:7000/');

Everything works well and I can send messages through the socket that are logged in the server.

This is the code I use on the server side:

let app = require("express")();
let http = require("http").Server(app);
let io = require("socket.io")(http);

io.on("connection", function() {
console.log("connected");
});

http.listen(port, function () {
  console.log("listening in port 3000");
});

The problem is that "connected" never logs when I run the same code on a remote server. Just the "listening in port 3000" part. I'm substituting the server's IP address and port in io.connect(). I even tried with the DNS name but it's still not connecting. I've checked the dependencies and they're the same as my local environment.

What could I be missing?

Top comments (4)

Collapse
 
brandinchiu profile image
Brandin Chiu

Where are you setting your port variable on your server side? Also note that your front end lists the port number as 7000, but your comment on the server mentions 3000.

Collapse
 
dmahely profile image
Doaa Mahely

Hello Brandin. My bad, I'm connecting to port 7000 on my local which works, but connecting to port 3000 on the remote server. I'm starting to think port 3000 is closed or something.

Collapse
 
nayhtoonaing48 profile image
NayHtooNaing48

Hello devs, I have the same problem with Unity Client and Node js + socket.io,
How to solve this problem ,

Collapse
 
dmahely profile image
Doaa Mahely

Hello there. Do you wanna post your code so we can take a closer look?