Braydon
Braydon

Reputation: 251

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

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:

Any help would be greatly appreciated!

Upvotes: 24

Views: 15406

Answers (4)

Marco Silva
Marco Silva

Reputation: 574

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.

Upvotes: 27

Tim Bijnen
Tim Bijnen

Reputation: 21

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

Upvotes: 2

Ashan
Ashan

Reputation: 19738

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.

Upvotes: 0

Warren Parad
Warren Parad

Reputation: 4072

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

Upvotes: -5

Related Questions