works only on localhost

Posted 3 years ago by zabi.zamir

I am trying to implement sockets by following the tutorial . 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('')(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,;
http.listen(3000, function(){
    console.log('Listening on Port 3000');

Client side:

<script src=""></script>
       /var socket = io('http://serverip:3000');
    socket.on("test-channel:App\\Events\\EventName", function(message){
        // increase the power everytime we load test route
        $('#power').text(parseInt($('#power').text()) + parseInt(;

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

GET http://serverip:3000/ Request.create @ @ @ @ @ @ @ @ @ @ @ function) @
feedTest:1 XMLHttpRequest cannot load http://serverip:3000/ No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access. The response had HTTP status code 502.

I also changed cdn 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

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.