22

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!

Braydon
  • 231
  • 1
  • 2
  • 6
  • It seems like the whole concept of serverless is contrary to a long running, stateful server process that maintains webSocket connections to clients. – jfriend00 Dec 13 '17 at 05:14
  • Thanks for the reply. I'm looking for a service that is the client that receives the new data pushed out from the websocket. That seems like a reasonable thing to wish to implement in a serverless framework. – Braydon Dec 13 '17 at 05:41
  • Any luck figuring this out? – andr111 Aug 05 '18 at 00:43
  • I am also looking form something like this, did you find anything useful ? – Marco May 05 '19 at 13:23
  • We also need this. I wish someone had built a service that converted websocket messages into webhooks that would be easy to consume via Serverless. – gduq Apr 19 '22 at 06:51
  • This would be awesome. Running an EC2 instance to listen to websockets seems overkill, AWS could provide a service that allows you to listen to websockets and run something (e.g add a message to SQS, make an HTTP call, etc) such that you'd pay for usage (e.g for every time your trigger got activated). It makes sense because AWS can use a single server for multiple customers, pretty much how it works with Lambda and other serverless services. – Alisson Reinaldo Silva Aug 11 '22 at 02:45

4 Answers4

27

If we are talking about to be the client, there's no solution/service, until now, on Amazon AWS that is serverless and stays alive just for the socket's living time (from connect to disconnect).

Unfortunately, I think we are left to work with instances for this kind of scenario.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Marco Silva
  • 564
  • 5
  • 15
2

I have spent quite some time finding a solution for the same problem but there doesn't seem to be a way to keep a websocket client running except for using an ec2 server.

The maximum running time of a Lambda function did increase to 15 minutes which could make it worth invoking a function that keeps connecting a client for up to 15 minutes.

https://aws.amazon.com/about-aws/whats-new/2018/10/aws-lambda-supports-functions-that-can-run-up-to-15-minutes/

using the pricing calculator it looks like it would be $5 at minumum to always keep a client running

Unit conversions Number of requests: 4 per hour * (730 hours in a month) = 2920 per month Amount of memory allocated: 128 MB x 0.0009765625 GB in a MB = 0.125 GB Amount of ephemeral storage allocated: 512 MB x 0.0009765625 GB in a MB = 0.5 GB Pricing calculations 2,920 requests x 900,000 ms x 0.001 ms to sec conversion factor = 2,628,000.00 total compute (seconds) 0.125 GB x 2,628,000.00 seconds = 328,500.00 total compute (GB-s) 328,500.00 GB-s x 0.0000166667 USD = 5.48 USD (monthly compute charges) 2,920 requests x 0.0000002 USD = 0.00 USD (monthly request charges) 0.50 GB - 0.5 GB (no additional charge) = 0.00 GB billable ephemeral storage per function Lambda costs - Without Free Tier (monthly): 5.48 USD

Tim Bijnen
  • 21
  • 3
0

API Gateway now supports WebSockets where you can handle the events in Lambda and also respond in a simplified way. For more information refer Announcing WebSocket APIs in Amazon API Gateway

enter image description here

Also there are two other 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.

Ashan
  • 18,898
  • 4
  • 47
  • 67
-4

API Gateway directly supports websockets. So you can connect you connect APIGW websocket api to a lambda function and connect this to DynamoDB.

Detailed step by step guide for setting up websockets in APIGW is available.

An example setup might be:

# 1. Create API Gateway Websocket
# 2. Create integration
aws apigatewayv2 create-integration 
  --api-id APIGW_ID --integration-type AWS_PROXY 
  --integration-method POST
  --integration-uri arn:aws:apigateway:REGION:lambda:path/2015-03-31/functions/LAMBDA_ARN/invocations
Warren Parad
  • 3,910
  • 1
  • 20
  • 29