askm3
askm3

Reputation: 59

node.js,socket.io only working on local , can't access from intranet systems

i am using node.js and socket.io for the real time notification system, so 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's working on my browser with this url http://localhost/schat/index.html but not working when i'm trying connect from another one system using my ip 192.171.56.23/schat/index.html but all other html files 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        

Upvotes: 1

Views: 4765

Answers (4)

Alex C.
Alex C.

Reputation: 1

Thank Karim, your solution work for me, in the 'server.js' configuration I set the 'port' value and the 'server', but not the ip, I use only the name without 'https://', like this:

// External route.
var express     = require("express");
var router      = express.Router();
// In cloud 9 use port 8081
const port      = 8081;
const webLnk    = 'mywebdevaddress.io';
// In dev local ambient 'localhost' work without the 'port' and the server, in prod internet // you need to specify the port and the link name.

const server    = require('http').Server(router).listen(port, webLnk);

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

Is the official socket.io docs I do not see any of this!

Upvotes: 0

Karim KARAA
Karim KARAA

Reputation: 101

I had the same problem and I fixed it :

Upvotes: 0

klode
klode

Reputation: 11051

Here is a working example from socket.io docs (slightly modified).

use <script src="/socket.io/socket.io.js"></script>

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

and use var socket = io.connect(window.location.origin);

instead of var socket = io.connect('http://localhost:8888');

(socket.io v1.3.5, express v4.12.2)

index.html

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect(window.location.origin);

    socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
</script>

server.js

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

server.listen(8888);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Upvotes: 1

Paramore
Paramore

Reputation: 1313

Change your client side code to

  <script>
       var url = window.location.host;
       var socket = io.connect(url);
  </script>

Upvotes: 0

Related Questions