Reputation: 59
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
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
Reputation: 101
I had the same problem and I fixed it :
First you have to change http://localhost:8888/ to http://your_IP_address:8888/ everywhere in your code.
Second, which took me too much time to fix was to change server.listen(8888); to server.listen(8888, "your IP address");
Upvotes: 0
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
Reputation: 1313
Change your client side code to
<script>
var url = window.location.host;
var socket = io.connect(url);
</script>
Upvotes: 0