简体   繁体   中英

Socket.io client cant connect to server

I have issue related to Socket.io connection to server. Its working fine on my local, but on dev-server it cant connect. My backend code look like this:

    var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');

server.listen(8080);

console.log('CONNECTED');
io.on('connection', function (socket) {
    var handshake = socket.handshake;
    console.log(handshake);
    console.log("new client connected");

    var redisClient = redis.createClient();

    redisClient.subscribe('notification');
    redisClient.subscribe('rate');

    redisClient.on("message", function(channel, message) {
        console.log("New message: " + message + ". In channel: " + channel);
        socket.emit(channel, message);
    });

    socket.on('disconnect', function() {
        redisClient.quit();
    });

});

And my client part like this:

 var socket = io.connect('http://localhost:8080');
 socket.on('notification', function (data) { console.log(data) }

The error that im facing is when socket.io client tries to send to URL " http://localhost:8080/socket.io/?EIO=3&transport=polling&t=MD_W4lE " request and its failing, with error ERR_CONNECTION_REFUSED. The node server is runing i tested and also tried to change localhost to 127.0.0.1 and ipv4 address, i didnt helped.

I think it's happening because of you haven't open node server port publically.

You can follow below step to open port on ec2 instance.

  • Go to the "Network & Security" -> Security Group settings in the left-hand navigation
  • Find the Security Group that your instance is apart of Click on Inbound Rules
  • Use the drop-down and add HTTP (port 3000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM