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

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


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


I don't think that Lambda is going to work for the case you described here. The link to the AWS forum below points out that the Lambda function can only run for a maximum of 15 minutes and further since you are charged per 100ms of function runtime this would probably be cost-prohibitive. There is a comment from Amazon saying they've heard the request several times so are interested in some way to allow for this.

https://forums.aws.amazon.com/thread.jspa?threadID=205761

Here is a post from someone who appears to have a good deal of success using EC2 and NodeJS but he had to use an alternative to Socket.io called Websockets/ws.

http://www.jayway.com/2015/04/13/600k-concurrent-websocket-connections-on-aws-using-node-js/

If you plan to run your server behind a load balancer it looks like you are going to have a few more hoops to jump through:

https://web.archive.org/web/20160118124227/http://coding-ceo.ghost.io/how-to-run-socket-io-behind-elb-on-aws


Update (since AWS re:invent 2018): API Gateway now supports websockets! See examples that use API Gateway websockets with Lambda here:

and documentation for this feature of API Gateway here: https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html

There's also an interesting example of a Node.js framework that uses socket.io with API Gateway, but I haven't investigated if it specifically will work for your use case: https://github.com/tiaod/moleculer-io


You should consider using Amazon IoT Core. I'll explain.

If you have a real-time situation where you need to perform a computation or leverage analytics on a real-time stream, you need to be thinking about streaming events (that reflect the state changes in real-time) to a platform designed for fast, high-availability event-streaming, such as a Kafka implementation like AWS Kinesis. Then you can consume the event-stream from a tool designed for real-time streaming analytics, such as Kinesis Analytics or Apache Spark or Apache Storm.

Then you can consume the streaming analytics (and optionally also additional event-provided data) using AWS Lambda (which can be triggered by events that come through your Kinesis pipeline) to push updates to all of the subscribers. You can push updates in real-time to these subscribers if wired up through the Amazon IoT Core service specifically if you create a "topic" for each user. The service is designed so that you don't have an upper limit on the number of topics you can have, so it should scale elastically.

This is an example of a best-practice "big-data" serverless (as long as you avoid maintaining VMs and only use serverless/managed services) approach to your problem, and it will be much more elastic, cost-effective, easy to maintain, and scalable than managing your own EC2 instances and needing to worry about all of the additional headaches with load-balancing and availability and replication and server-state and idempotency and scaling and wasted resources and the deployment pipeline and instance monitoring, etc., etc..

You can even push events directly to the client browser with web sockets over MQTT (which is very fast and lightweight) if you use the Amazon IoT Core service, and you can integrate it directly with AWS Lambda. There's a great demo app that uses IoT Core here: https://github.com/aws-samples/aws-iot-chat-example

Personally, I prefer the approach that is less expensive, easier to maintain, performs better, allows me to get sleep at night, and allows me to get uninterrupted sleep that is free of nightmares.