Is it possible to subscribe to a WebSocket in a serverless fashion using AWS?

Braydon picture Braydon · Dec 13, 2017 · Viewed 9.9k times · Source

A website offers a websocket to get real-time data from. I'm trying to record data received from the websocket in a DynamoDB table for a data source for a serverless application. Their example of how to use the websocket is some Node.JS code using socket.io-client. Being JavaScript I thought to use an AWS Lambda function but they are not purposed to run constantly. Is there an AWS service to handle this sort of subscription? I don't want to make a small EC2 instance to run a tiny application just for this purpose.

Things I've looked at:

  • Lambda functions - Only for short executions not long running tasks.
  • SNS subscriptions - From what I understand this needs to subscribe to an SNS publisher.
  • Kinesis Stream - Requires producer to use Streams PUT API.

Any help would be greatly appreciated!

Answer

Ashan picture Ashan · Dec 13, 2017

You cannot use Lambda directly to keep a Websocket connection since Lambda and API Gateway is stateless. There two services in AWS you can use to achieve this,

  • AWS IOT Websockets:- The idea is clients will subscribe to IOT Topics and from Lambda it will push messages to client.

enter image description here

  • AWS AppSync (Newly Introduced):- Having a layer in between Lambda and DynamoDB which will provide WebSocket support.

Note: You might need to request for the preview of AppSync at the moment.