Sunday, 9 April 2017

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.



via Ritwik Garg

No comments:

Post a Comment