How to use Socket.io in aws lambda functions - node.js

I'm using node js with aws lambda functions for my web application. I want to use web socket in my web app. And socket.io is a very famous library for node.js.
But I'm not sure how can I use it in aws lambda. can anyone let me know how to do it, is it possible using lambda functions?

Can't use socket.io with lambdas. Lambdas have a limit on time it can be executed, so holding open connection with a client is impossible.
What you can do though is use SNS. Lambdas can publish messages to SNS topics and lambdas can be invoked with SNS.

Workaround exists for this - it's the WebSockets over MQTT in AWS IoT. This way you can execute your Lambda functions from open socket connections.

Currently, AWS API Gateway supports WebSockets.
Unfortunately, I didn't manage to connect via socket.io since it generates a custom URL with additional params: /?EIO=3&transport=polling&sid=< id>
But I've found tiny WebSocket wrapper sockette (used this tutorial), and it works fine!

Related

What is the best Socket.io alternative in AWS serverless

I'm building a custom serverless chat on AWS.
I want to use WebSockets for updating the messages.
I found that Socket.io is amazing for what I need and it also supports heartbeat as fallback if WebSockets does not work.
My problem with Socket.io is that is does not have server for AWS ApiGateway.
Is there any library that will replace Socket.io for me on serverless?

When should I be opening and closing websocket connections for a chat inside of another app?

I'm using Serverless, AWS API Gateway and AWS Lambda to attempt to send real-time messages and there's a few examples I've found that nail this concept. I'm having issues wrapping my head around where WebSocket connections should be opening in my app.
I have created a GraphQL API and a multi-page and I would like to have a real-time chat app embedded into it across all pages, similar to your Facebook or WhatsApp or whatever. I have implemented GraphQL mutations and queries to make Threads where Users and Messages would reside in and those have their own mutations and queries as well and I also have 4 Lambda functions set up. One for the GraphQL server, and for WebSockets I have open, close and sendMessage.
I thought about opening a WebSocket connection whenever the GraphQL server gets executed but then I feel like this would keep the Lambda up until the connection closes which I think is against the Serverless framework philosophy and best practices in the sense that the Lambda function would have to wait for the WebSocket connection to close for the Lambda to finish its execution. But at the same time, I feel like for a real-time chat app to work on another app across all pages, it should stay up? Are there any other options in the back-end where I could possibly be opening the connection?
The other alternative is to connect it on the front-end side of things?

(ASK) How we debug local websocket with AWS api gateway

I need to test my functions locally using AWS Web Socket API Gateway. Is there any possible way to mimic or duplicate the behaviour of AWS Web Socket in my local machine ? So I don't need to deploy every little changes to my Lambda function
Thanks
Unfortunately we're still waiting on local sam support for v2 of API Gateway.
https://github.com/awslabs/aws-sam-cli/issues/896
Recently I was able to find here that there is a workaround available called aws-lambda-ws-server which wraps the routes with a websocket server when ran locally and also works deployed as a AWS Lambda.
Hopefully this will help others in the future!

How would one run a Node.js script on Firebase?

I have a Node app/script that needs to constantly be running (it's a discord bot, done with discord.js, but I think that's mostly irrelevant), and I'd like to do it on Firebase.
It has its own client.on('event', ()=>{}) events system, so I don't believe that I could use Firebase's cloud functions. There's also what seems to be a website-hosting based way to have a node.js server, but that seems triggered by HTTP requests.
Is there any other way I could do it?
There is no way to run arbitrary node.js code on Firebase. Unless your script can run within Cloud Functions "triggered execution" mode, you'll need your own app server to run it.
You can of course create a service that maps Discord.js events to Firebase events, such as writes to the Realtime Database, Cloud Firestore, even just direct HTTPS calls to a Cloud Functions endpoint. You could even bypass Firebase there and have your mapping service write to Cloud PubSub and use that to trigger Cloud Functions.
One thing that looks promising in the Discord.js documentation is their mention of web hooks, which is just another way of describing HTTP endpoints. But from my quick scan I couldn't figure out if those would allow you to call your HTTP triggered Cloud Function.

how to develop socket like approach with aws lambda (serverless architecture)

How to develop/refactor a node.js project that uses socket.io and send JSON data from a client to another in a room?
I've developed a project with node js and socket.io that there is a board game in a part of it, the user1 select a place and its information send to the user2 in JSON format and this part developed with socket.io.
now I want to deploy the project to AWS Lambda but I realize that socket.io can't run because of serverless nature, so my question is how can I develop this board game with AWS Lambda and what technology/library can I use?
You cannot do socket.io like bi-directional communication only using AWS Lambda since it is stateless and asynchronous. You can use AWS IOT Web Sockets for this(Optionally with Lambda).

Resources