Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EC2 with socket.io

I have set up an aws micro instance for my node application. I use socket.io as well. I am getting the following error:

GET http://localhost:3000/socket.io/1/?t=1393065240268 net::ERR_CONNECTION_REFUSED

in the console at the moment when the socket connection should be created. Apart from this the node app works. I suspect that the GET should not be towards localhost but towards the address of the server.

Note that on the server side node logs that it served socket.io:

debug - served static content /socket.io.js

Here is a picture of the Security Group of my server:

Security Group.

Socket.io setup:

env = process.env.NODE_ENV || 'development',
packageJson = require('../package.json'),
http = require('http'),
express = require('express'),
RedisStore = require('connect-redis')(express),
SessionSockets = require('session.socket.io'),
path = require('path'),
settings = require('./settings'),
expose = require('express-expose')
//Configure server for io and session.socket.io
tmpApp = express(),
tmpServer = http.createServer(tmpApp),
io = require('socket.io').listen(tmpServer),
appCookieParser = express.cookieParser(settings.cookie.secret),
appRedisStore = new RedisStore(),
sessionIO = new SessionSockets(io, appRedisStore, appCookieParser)

global.App = {
    app: tmpApp,
    server: tmpServer,
    port: process.env.PORT || 3000,
    sessionIO: sessionIO,
    io: io,
    start: function() {
        var setUp = this.util('setUp'),
            socketHandler = require('./socketHandler'),
            self = this

            setUp.initialize(function(err, waitingGames) {
                if (err) {
                    console.log('error at initializing the application')
                    process.exit(0)
                }
                if (!self.started) {
                    self.started = true
                    self.server.listen(self.port)

                    socketHandler()

                    console.log("Running App Version " + App.version + " on port " + App.port + " in " + App.env + " mode")
                }
            })

    },
 ...
 }

UPDATE

When I changed my port to 80 I get a different error:

 XMLHttpRequest cannot load http://localhost/socket.io/1/?t=1393067003774. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ec2-54-214-136-70.us-west-2.compute.amazonaws.com' is therefore not allowed access. 
like image 832
Pio Avatar asked Feb 22 '14 10:02

Pio


1 Answers

I found the problem. It was on the client side. I was connecting to localhost. It's a stupid error, but during development you don't pay attention to these details and it seemed natural that socket.io should connect to the root from where you serve your content.

Since I'm using EC2 and after each restart I get different DNS address I've sent to the page where I'm initializing the socket.io the correct the req.headers.host (using express-expose).

like image 143
Pio Avatar answered Oct 06 '22 09:10

Pio