EC2 with socket.io
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
).
Pio
"A day without sunshine is like, you know, night." SOreadytohelp
Updated on June 04, 2022Comments
-
Pio almost 2 years
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 towardslocalhost
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:
.
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.
-
Data almost 10 yearsthank you, thank you :)) I was also using localhost and when i changed it to my url, it worked.
-
Guido Tarsia over 8 yearsIt should be simpler to set the socket.io server address as '/socket.io' which should reference the server I'm getting the website from. Not sure if socket.io handles that, but it definitely should.