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

后端 未结 8 1565
深忆病人
深忆病人 2021-01-30 01:31

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 SuperPh

8条回答
  •  半阙折子戏
    2021-01-30 02:06

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

    • https://serverless.com/blog/api-gateway-websockets-support/
    • https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/

    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.

提交回复
热议问题