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

zabi.zamir's avatar

Socket.io works only on localhost

I am trying to implement sockets by following the tutorial https://laracasts.com/discuss/channels/general-discussion/step-by-step-guide-to-installing-socketio-and-broadcasting-events-with-laravel-51 . I got node.js running, reddis working and port 3000 is listening. I can get console messages on command line. Problem occurs when i try to emit anything to the client side. Sever side:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel', function(err, count) {
});
redis.on('message', function(channel, message) {
    console.log('Message Recieved: ' + message);
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});
http.listen(3000, function(){
    console.log('Listening on Port 3000');
});

Client side:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js"></script>
<script>
       /var socket = io('http://serverip:3000');
    socket.on("test-channel:App\\Events\\EventName", function(message){
        console.log("Working");
        // increase the power everytime we load test route
        $('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
    });

It works perfectly fine on locahost, but from external ip it gives me Access-Control-Allow-Origin 502 Error.

GET http://serverip:3000/socket.io/?EIO=3&transport=polling&t=LDrVJCA Request.create @ socket.io.min.js:1Request @ socket.io.min.js:1XHR.request @ socket.io.min.js:1XHR.doPoll @ socket.io.min.js:1Polling.poll @ socket.io.min.js:1Polling.doOpen @ socket.io.min.js:1Transport.open @ socket.io.min.js:1Socket.open @ socket.io.min.js:1Socket @ socket.io.min.js:1Socket @ socket.io.min.js:1Manager.open.Manager.connect @ socket.io.min.js:2(anonymous function) @ socket.io.min.js:3
feedTest:1 XMLHttpRequest cannot load http://serverip:3000/socket.io/?EIO=3&transport=polling&t=LDrVJCA. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access. The response had HTTP status code 502.

I also changed cdn socket.io.min.js and stored locally as few people suggested that. I have been trying every single suggestion and example available online but nothing helped. Any help would be much appreciated. Thanks

0 likes
14 replies
willvincent's avatar

You should load the client-side socket JS library from your node server, rather than from a cdn. socketio will automagically serve the client JS file for you.

<script src="http://[serverip]:3000/socket.io/socket.io.js" type="text/javascript"></script>
<script>
  var socket = io('http://[serverip]:3000');
  // do stuff
</script>

[serverip] obviously should be the domain or IP your node server is running on.

You may also need to set access control headers, I think this ought to work:

    app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Headers", "Content-Type");
        res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
        next();
    });
midascodebreaker's avatar

if your using forge go to you dashboard ->server->daemon add the following

redis-server --port 3000 
// this is optional redis is started automatically in port 3001

and

node /home/forge/yourdomain.com/socket.js

this 2 daemon will ensure your port is running of port 3000, and socket.js is running but by default even without using redis-server --port 3000 , redis is automatically run during server restart in port 3001

then in your dashboard->server->network you may want to specify what port you would want to open give an alias Redis then specify port to allow in the firewall

finally if it is still making some error ike socket.js is timing out you can config your iptables ssh into your server then run this command l

iptables -I INPUT 1 -i eth0 -p tcp --dport 3000 -j ACCEPT

This will work ive tested it myself in forge

zabi.zamir's avatar

@willvincent Thank you for your reply, I have changed my code to this:

<script type="text/javascript" src="http://[serverip]:3000/socket.io/socket.io.js"></script>
<script>
var socket = io( 'http://[serverip]:3000' );

console.log('check 1', socket.connected);
socket.on('connect', function() {
  console.log('check 2', socket.connected);
});

socket.on("connect", function () {  
    console.log("Connected!");
});

And now I get this error:

GET http://[serverip]:3000/socket.io/socket.io.js net::ERR_CONNECTION_TIMED_OUT

I assume that node.js server does not serve the file. I could not locate file manually, I could only locate node_modules/socket.io-client/socket.io.js but then I get timeout error as well.

Please help me as I his issue is driving me mad. I ma trying to solve it for a week already. Thanks

zabi.zamir's avatar

@veve286 thanks for reply. Port 3000 is listening as I checked it via netstat -an and it work perfectly fine on localhost. Do you have any other suggestions? Thanks @supervip thanks for reply but I am not using forge. Do you have any other suggestions? Thanks

midascodebreaker's avatar

run node socket.js your getting that error time out connection cause its not running

zabi.zamir's avatar

@supervip node sockets.js is running as it works on localhost. @veve286 I know that server ip is actual ip, domain name does not make a difference. I will try forever package after I will solve this issue. Do you have any more suggestion? Thanks

willvincent's avatar
Level 54

If the client library isn't being loaded when you tell it to pull from the /socket.io/socket.io.js path on the node server, that's indicative of your connectivity issue, nothing else. That route is automatically mapped and made available by the socket.io server side node module.

Questions:

  • Where are you running the node server?
  • Where are you running your webserver for the rest of the site's code?
  • Are those running from the same location?
  • You keep saying "it works fine on localhost" -- is the node server publicly accessible, or are you actually just running it locally?
zabi.zamir's avatar

@willvincent you a life saver. Your questions made me thinking and apparently port 3000 was closed to publicity. That was my problem. Now it is working fine from anywhere. Thank you very much. I couldn't do it without you.

noviric's avatar

how did you do it @zabi.zamir, did you configured your web-server? i have the same issue as yours, could you help me

tiagomatosweb's avatar

I sort this out. The server is hosted on Google and it needed to allow the port on Google panel. :(.

Actually it was a workmate that has found out.

Please or to participate in this conversation.