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.
Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.
ALB provides native support for WebSocket via the ws:// and wss:// protocols. HTTP/2 is a significant enhancement of the original HTTP 1.1 protocol. The newer protocol feature supports multiplexed requests across a single connection.
The Classic ELB doesn't support Websockets.
The load balancer knows how to upgrade an HTTP connection to a WebSocket connection and once that happens, messages will travel back and forth through a WebSocket tunnel. However, you must design your system for scale if you plan to load balance multiple WebSocket servers.
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With