Websockets from Ions

Has anyone got any advice on working with websockets in a Datomic Ions environment? A few questions

  • Is it possible to open a websocket connection to a browser, from code running inside an Ion?

  • If yes, thinking ahead to scaling up to multiple compute instances, there is the inevitable complication that code running in one instance may need to send a message on a websocket that was opened in another instance. Are there standard solutions?

  • If the best answer is “let AWS take care of it”, any pointers regarding which services and APIs to look at?

Thanks very much

Tom

Hi Tom,

I’m currently using Datomic Cloud together with an API Gateway WebSocket API. API Gateway provides a managed WebSocket service where it handles all the connections and proxies the individual actions to your service.

The implementation was pretty easy. I have a Web Ion which is invoked by API Gateway when a client connects, disconnects, or sends data through the socket connection. With API Gateway, I can publish data to any connection via an API call with using the connection id it provides. This solves the problem you mentioned on your second bullet point.

Cheers,
Furkan

1 Like

This is great news. Thanks very much for posting.

I can publish data to any connection via an API call with using the connection id it provides

Could you point me to the API in question? Also a code snippet showing how to obtain the client-id would be useful.

Thanks again!

Hi,

This is the API you can use to send messages to the connections via API Gateway.

You can get the connection id when a new client creates a socket connection via the $connect route of API Gateway. You can trigger an ion for $connect route and save the connection id for later use.

When you create an ion and proxy the WebSocket events from API Gateway, you’ll get :datomic.ion.edn.api-gateway/data in the request, just like with the regular HTTP ions. Within that data, you can find :connectionId under :requestContext.

Cheers,
Furkan

1 Like