Node.js, socket.io only working on localhost, can’t access from intranet

node.jssocket

I am using node.js and socket.io for the real time notification system. I have tested node.js and socket.io with simple chat code, it pretty good with localhost but can't access the same from the another system which are connected locally with same network. My server and client code looks like below:

server.js

var express = require('express')
 , app = express()
 , http = require('http')
 , server = http.createServer(app)
 , io = require('socket.io').listen(server);

 server.listen(8888);

and client html index.html

<script src="http://localhost:8888/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
    var socket = io.connect('http://localhost:8888');
</script>

It is working on my browser with this url http://localhost/schat/index.html, but not working when I am trying to connect from another one system using my ip 192.171.56.23/schat/index.html. But all other html files are working fine, below is my netstat output:

[root@localhost schat]# netstat -pan | grep 8888
tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN        8068/node
tcp        0      0 127.0.0.1:8888          127.0.0.1:38273         ESTABLISHED 8068/node
tcp        0      0 127.0.0.1:38273         127.0.0.1:8888          ESTABLISHED 7990/firefox

Best Answer

After some above comments :

<script>
    var socket = io.connect('http://localhost:8888');
</script>

To be able to run your Node client on any remote machine, you have to change localhost with the actual IP of the server

Same thing for:

<script src="http://localhost:8888/socket.io/socket.io.js"></script>