Skip to content

API Gateway Websocket Binary Frames

0

Hi All,

My customer has been making use of API Gateway's Websocket integration, performing two way communication with a lambda function.

They've previously been using JSON but would like to send binary frames.

I understand that AWS receiving binary frames from the client is not supported, with the client being disconnected as described in the documentation.

AWS sending binary frames to the client is slightly less well described. It appears to be supported with the REST interface option but the websocket status is less clear.

We've tried both the proxy and non-proxy integration options.

The closest we've come to getting things working is non-proxy integration, with an integration response content handing strategy of CONVERT_TO_BINARY. When enabled, if the lambda replies with base64 this leads to a client side UTF8 error. Which appears to be the API gateway sending the binary response to the client in a text type, rather than binary type websocket frame.

I'm tending to think that aside from base64 encoding in either direction that binary isn't supported via websockets. The packet type must always be text.

Any pointers to transfer binary data over a websocket would be appreciated, likewise confirmation that this isn't possible.

Thanks,

Phil

1 Answer
1
Accepted Answer

You're correct in your understanding of the current limitations regarding binary frames in API Gateway's WebSocket integration. Let me clarify the situation based on the available information:

  1. Receiving binary frames from clients: API Gateway WebSocket APIs do not currently support binary frames in incoming message payloads. If a client sends a binary frame, API Gateway will reject it and disconnect the client with code 1003.

  2. Sending binary frames to clients: The situation for sending binary data from the server to the client is indeed less straightforward. While there isn't explicit support for sending binary frames directly over WebSockets, there are workarounds available.

For your specific case, using a non-proxy integration with the integration response content handling strategy set to CONVERT_TO_BINARY is a step in the right direction. However, the UTF-8 error you're encountering suggests that the binary data is still being sent as a text frame rather than a binary frame.

To work around these limitations, here are some approaches you can consider:

  1. Base64 encoding: Instead of trying to send actual binary frames, you can have your Lambda function return base64-encoded data. On the client side, you would need to decode this base64 string back into binary data. This approach ensures that all communication remains text-based, which is fully supported by API Gateway WebSockets.

  2. Custom encoding: You could implement a custom encoding scheme that represents your binary data as text. This might be more efficient than base64 for certain types of data.

  3. Chunked transfer: For large binary payloads, you could split the data into smaller chunks, send them as text frames, and reassemble them on the client side.

  4. Alternative protocols: If binary frame support is crucial and cannot be worked around, you might need to consider alternative solutions like using WebRTC data channels or a different WebSocket server implementation.

In conclusion, while API Gateway WebSockets do not natively support binary frames in either direction, you can still transfer binary data by encoding it as text. The limitation is that the WebSocket frames themselves must always be text frames. If you need true binary frame support, you may need to explore alternative solutions outside of API Gateway WebSockets.
Sources
Binary media types for WebSocket APIs in API Gateway - Amazon API Gateway
WebSocket APIs: Showcasing a Practical Networking Solution | AWS Spatial Computing Blog

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.