Websockets with socket.io on AWS Elastic Beanstalk
Asked Answered
P

4

30

I'm trying to setup a websocket-server with socket.io on AWS Elastic Beanstalk. Got the initial request to /socket.io/1/?t=xxxx up and running but the websocket handshake fails with this error:

WebSocket connection to 'ws://xxxxxx.elasticbeanstalk.com/socket.io/1/websocket/a4DTCN2BeJZuiSaphVyz' failed: Unexpected response code: 502 

From googling and looking at the error 502 (Bad Gateway) it seems to have something to do with the nginx-proxy. So how do I configure the nginx to work with websockets?

Can't find anything in AWS documentation about setting up for websockets.

Pious answered 9/4, 2013 at 16:22 Comment(2)
Got it working by changing the proxy from nginx to none.Pious
I've also seen the same issue reporting (the dreaded) "Error during WebSocket handshake: Unexpected response code: 400"Strapped
L
28

you could try to go through the trouble of configuring nginx, but if you are only using websockets, it would be easier to turn off all proxys and directly connect.

this is under your app environment in the elastic beanstalk dashboard. There's a drop down button on the right marked "Actions". Select "Edit/Load Configuration"

enter image description here

Luciana answered 17/4, 2013 at 15:37 Comment(7)
As of 2014-04-13 this is located under: <Application Name>/<Environment name>/Configuration/Software Configuration/Container Options.Decrescendo
Does "easier" also mean better? Like, what do you get by setting the proxy server to nginx (besides static file mappings and gzip compression, both of which I don't need). What about performance & security? I'd suspect performance is better with no proxy server. But, what about security? Is nginx better for WSS (WebSockets over SSL/TLS), or should I just implement SSL with Node.js? What about DoS protection? Anything else?Dhiman
I've proxy server off, but I'm getting an error connection is closed before it's established, question posted here: #26880896Isbella
Where can you turn off nginx? It's no longer under Container Options.Josefajosefina
I think the accepted answer should include an solution without turning off nginx.Hygiene
To get this to work I also had to update the load balancer to use tcp. github.com/socketio/socket.io/issues/1846#issuecomment-61038664Commence
This doesn´t work anymore, as of 2017-02-03 Currently only the Nodejs container allows you to turn-off the proxy on the instance.Calycine
P
4

There is a significant disadvantage with the ditch-nginx approach. You will have to move https into node and also to your socket.io calls. It's much easier to manage https in the server configuration rather than in node.

If you don't need https I suggest dumping the proxy and just going directly to the node.

Petiolate answered 29/11, 2015 at 2:25 Comment(1)
This is incorrect, you can add a certificate directly on the Load Balancer.Josefajosefina
C
4

Socket.io defaults to port 8080. Make sure port 8080 is enabled on your EC2 Security Group by adding a "Inbound > Custom TCP Rule" in the AWS Admin > EC2 > Security Groups.

Creepy answered 24/8, 2016 at 20:20 Comment(0)
M
0

This error can be fixed by adding a new listener to the load balancer. You can add a new listener with TCP protocol like below.

enter image description here

Masha answered 10/10, 2023 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.