Ritwik Garg
Ritwik Garg

Reputation: 73

Unable to get socket.io working on AWS server

I have a server hosted at Amazon Web Services. I am using socket.io with nodejs on my website. Following is the code: Client Side-

function bindSocket(){
iosocket = io.connect('http://ec2-54-190-34-106.us-west-2.compute.amazonaws.com:8080');
iosocket.on('connect', function () {
    alert('connected');
    iosocket.on('message', function(message) {
        //alert(message);
       getNotificationData(message);
        //document.getElementById("socket_div").innerHTML = message;
    });
    iosocket.on('disconnect', function() {
        //alert('disconnected');
    });
});
}

Server side-

var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
//res.end(fs.readFileSync(__dirname + '/socket_test.html'));
res.end();
}).listen(8080, function() {
console.log('Listening at: http://localhost:8080');
});

socketio.listen(server).on('connection', function (socket) {
console.log('new connection');
socket.on('message', function (msg) {
    console.log('Message Received: ', msg);
    socket.broadcast.emit('message', msg);
});
});

I get the following error message:

polling-xhr.js:261 GET http://ec2-54-190-34-106.us-west-2.compute.amazonaws.com:8080/socket.io/?EIO=3&transport=polling&t=LjIkBx8 net::ERR_CONNECTION_TIMED_OUT

I have been using the same code(with IPs changed) on digitalocean server. However, I migrated to AWS and I'm unable to get it working.

Any help would be highly appreciated.

Upvotes: 0

Views: 1874

Answers (2)

Codemaker2015
Codemaker2015

Reputation: 1

You might not enable the socket ports in the AWS network security group. Also, you have to add the same ports in the ufw (firewall) list.

  • Open the inbound rules and add the custom port to the list. enter image description here

  • Type the following command in the terminal to add the custom port in the ufw list.

    sudo ufw allow 5005/tcp
    

NB: the port (5005 in the example) may vary according to your implementation.

Upvotes: 0

Ritwik Garg
Ritwik Garg

Reputation: 73

I figured it out. The port itself had to enabled from the AWS security group. I added the rule and everything worked. Hope this helps someone else.

Upvotes: 2

Related Questions