Socket.io client connection through AWS Api Gateway
See original GitHub issueHello. I am trying to create a connection like this: socket.io-client -> AWS Gateway API.
While trying to connect from socket.io client to an AWS Gateway API, connection can’t be established.
It stucks with a status ‘101 switching protocols’ for a few seconds, then throws ‘disconnected’ event and tries to reestablish connection, and the process repeats infinitely.
This doesn’t happen with native WebSockets or ‘ws’ library though, everything connects like it should.
I think issue has something to do with how socket.io client and server sides interact with each other by exchanging certain messages which may not be generated on the AWS side of things.
So my question is: is that even possible to establish a connection like this without using proxies:
socket.io-client -> AWS Api Gateway -> socket.io(node.js)
,
or it always has to be Socket.io-client -> socket.io(node.js)
?
Amazon AWS documention was used for configuring gateway: https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/ It uses standart $connect, $disconnect and $default routes, but instead of lambda its nodejs backend. The issue though is only on the client side.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (1 by maintainers)
Top GitHub Comments
I spent a bit of time checking the integration of socket.io with AWS API gateway and web socket. out-of-the-box socket.io client doesn’t work because the protocol used by socket.io is custom and not the standard websocket one.
As specified here (bottom page):
Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a 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.
Moreover, I checked the internal implementation of the socket.io server and I was able to attempt the connection but it failed, it’s not just a simple websocket wrapper.Alternatives:
just change the SOCKET_URL with the wss URL provided by API gateway and it is going to work (as long no authentication is required, if you need authentication as well you will need a bit of more code for providing what API gateway is looking for - either API key or token).
I hope this helps 😃
@JerryLeeCS I ended up using native WebSockets on a client side and $connect, $disconnect and $default routes on a backend side. I didn’t find any solution to make it work with Socket.io. Seems like Socket.io connections from the frontend are not supported by AWS Gateway API, it just refuses to connect.