Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vinoth06's avatar

Socket.io Not working in live server Ubuntu

Hi

Am trying with socket.io and I made successful run on localhost. But when I tried to implement the same on server it doesn't work.

Server Code
var server = require('http').Server();

var io = require('socket.io')(server);


server.listen(4000, '0.0.0.0', function () {
    console.log('listening');
});

io.on('connection', function(socket) {

    console.log('Client connected.');

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


Client Code

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script>
//for localmachine
    var socket = io.connect(':4000');
//for server
var socket = io.connect(':4000');
    console.log(socket.connected);
    console.log(socket.connecting);
    socket.on( 'connection', function () {
        console.log( 'connected to server' );
    } );

    console.log(socket);
    socket.on('connect_error', handleNoConnect);
    socket.on('connect_timeout', handleNoConnect);
    socket.on('connect', onConnect);

    function handleNoConnect() {
        console.log("No connection");
    }

    function onConnect() {
        console.log("connected");

// set other event handlers on a connected socket
        socket.on('disconnect', function() {
            console.log("disconnected");
        });
    }

    socket.on('connection',function (data) {
        console.log(data);
    });
</script>

Though I checked the 4000 port is listening in IPV4

COMMAND  PID       USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
node    9899 buffercode   12u  IPv4 3246821      0t0  TCP *:4000 (LISTEN)

Am getting No Connection.

Kindly suggest me where I did the mistake

0 likes
9 replies
Thyrosis's avatar

Assuming this is a remote Ubuntu server, could it be that the firewall doesn't allow connections on port 4000? Node could listen all that it wants, but if this requests don't even get to the server, nothing will happen.

biishmar's avatar

@vinoth06 In server.listen put function in the second parameter and delete the ip address..

in console, is "listening" displaying when u run that page?

vinoth06's avatar

@Thyrosis Yes its remote Ubuntu server. No I have opened the port 4000 in plesk panel and still the same issue

Allow incoming from all on ports 3000/tcp, 4000/tcp

Is there any way to know whether the requested reached the port 4000, though i have checked the Apached log and error file, no clue :-(

vinoth06's avatar

@biishmar : Yes even I have tried like you advised, if i do then its listening in only IPV6 format

tcp6       0      0 :::4000                 :::*                    LISTEN      3056/node
Thyrosis's avatar
Thyrosis
Best Answer
Level 17

@vinoth06 you could use a telnet session from your local machine to try and reach it. I'm guessing this is not the issue, but it can't hurt to check.

You can do it with any terminal in Windows (needs telnet enabled through the extra Windows options), Linux or Mac:

telnet yourhostnameorip 4000

It should look like this:

// This one doesn't connect
$ telnet google.com 4000
Trying 216.58.211.110...

// This one does connect
$ telnet google.com 80
Trying 216.58.211.110...
Connected to google.com.
Escape character is '^]'.
xds74's avatar

@vinoth06 hi vinoth, i have the same problem. Can you explain to me how you solve the pb. I've been trying for several days, I can't get io to work. in localhost it does not work in https. is my port configured correctly? ACCEPT tcp -- "ip_my_server" anywhere tcp dpt:3300

Is there any other manipulation, files to configure?

Thanks if you can help me.

2 likes
shofick's avatar

@xds74 hey I am facing the same issue can you explain how did you solve the problem I am using Ubuntu vps and nginx tried everything from a week if I chaged client flutter socket version to 1.0.0 then only the connection and disconnected event work not others event but in localhost work everything fine in 1.0.0 and also with 2.0.2 don't know what is happening and yes in nmap scan 3000 port is closed and nodejs listen the socket on 3000 but i setup nginx with location string so with http 80 port he can redirect to localhost:port

Thyrosis's avatar

You're very welcome, glad you got it to work!

Please or to participate in this conversation.