EC2 with .io socket

I created an aws micro instance for my node application. I am using socket.io . I get the following error:

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

in the console when the socket connection is created. In addition, the node application works. I suspect that the GET should not be in the localhost direction, but to the server address.

Notice that on the server side, node says that it served socket.io :

 debug - served static content /socket.io.js 

Here is a photo of my server’s security group:

Security group .

Configure Socket.io:

 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 another 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. 
+7
amazon-web-services amazon-ec2 sockets
source share
1 answer

I found a problem. It was on the client side. I connected to localhost . This is a stupid mistake, but during development you do not pay attention to these details, and it seemed natural that socket.io should connect to the root where you are serving your content from.

Since I use EC2, and after each restart, I get a different DNS address that I sent to the page where I initialize socket.io correct req.headers.host (using express-expose ).

+12
source share

All Articles