Is it possible to use Socket.io with AWS Lambda?

Vladislav picture Vladislav · Nov 25, 2015 · Viewed 43.3k times · Source

Is it possible to build a function in AWS Lambda that creates a websocket and send data to subscribed applications?

Something like this:

John has the app SuperPhotoApp opened in his phone but decides to use the desktop browser to upload a photo to the SuperPhotoApp service (a S3 Bucket), this event executes a Lambda function that creates a socket.io server and pushes the update to all subscribers, his phone had the app opened so the app automatically updates with the new photo.

This is something that can be done with push notifications or Amazon SNS, but what if I need real-time behaviour for example an online game where I need to update the position of a character.

If this is not possible with Lambda, is there any solution where I can update my opened app using a desktop browser?

Amazon EC2 is the only option? I've read that it has problems with scaling, that's why I'm commenting on Lambda.

Answer

Dmitriy Nevzorov picture Dmitriy Nevzorov · Jul 11, 2016

Recently AWS released support of WebSockets for IoT service. It is very easy to use as Pub/Sub message system for serverless web applications. You can post new messages from AWS lambda function via http post request and receive them as websocket messages on a client.

I wrote a small npm package that handles websocket connection to MQTT server from the front-end app. Check out aws-mqtt-client