question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Socket.io client connection through AWS Api Gateway

See original GitHub issue

Hello. 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
lucamezzaliracommented, Jun 20, 2021

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:

  1. using another client library or native websocket implementation. Here a basic implementation with the native websocket APIs:
const socket = new WebSocket(SOCKET_URL);

socket.addEventListener('open', function (event) {
    console.log("socket open")
    console.log(event)
});

socket.addEventListener('message', function (event) {
    console.log('Message from server ', event.data);
});

socket.addEventListener('error', function (event) {
    console.log(event)
});

socket.addEventListener('close', function (event) {
    console.log("socket close")
});

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).

  1. another alternative that I spotted in my researches but I didn’t try myself is creating a socket.io server on a ec2 or a container and expose via API gateway, in that way they can use socket.io because the protocol implemented between client and server will be compatible clearly.

I hope this helps 😃

5reactions
Sairysscommented, Sep 29, 2020

@Sairyss is there an update on this?

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS API Gateway integration with Socket.io - Stack Overflow
First, the API Gateway using Cognito to authenticate your client;. Second, assuming you are using an EC2 running Node.JS with Socket.IO using ......
Read more >
Working with WebSocket APIs - Amazon API Gateway
A WebSocket API in API Gateway is a collection of WebSocket routes that are integrated with backend HTTP endpoints, Lambda functions, or other...
Read more >
Creating realtime WebSocket connection using AWS API ...
The WebSocket URL is used to send messages from client to API Gateway, and the Connection URL is used other way round. I...
Read more >
Amazon API Gateway vs Socket.IO - Ably Realtime
Connection state recovery (stream resume). In the case of unreliable network conditions, clients may suddenly disconnect.
Read more >
Building Real-Time Serverless Web Applications with AWS ...
In 2018 AWS released WebSocket API as a new offering for API Gateway. WebSocket API is a 100% serverless solution to managing client...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found