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.
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.