r/node • u/PrestigiousZombie531 • Dec 28 '23
What happpens to this function when there are 10000 people connected?
244
u/MaxUumen Dec 28 '23
First problem I see is you are stringifying the same exact object every iteration.
56
u/PrestigiousZombie531 Dec 28 '23
sir that is a good one!!!!!!! i have no idea how the hell i skipped that lool
28
u/MaxUumen Dec 28 '23 edited Dec 29 '23
Ideally, if you really have so high traffic, you would own all the code as close to the socket as possible so you could find and eliminate as much repeated work as you can. You'd probably even be able to cache the Buffer object sent into some deeper interface. And at some point, a for loop will become faster than a looping method. But at that point I'd question whether it's easier to add a few more instances since you would have to decentralize it somehow anyway. For example, Redis streams to broadcast same message to all BE instances handling few thousand connections each etc.
8
u/PrestigiousZombie531 Dec 28 '23
i would love to use for loop but ws.clients are a Set not an Array not sure how else to iterate over that. How will redis streams work with websocket protocol?
16
u/cazzer548 Dec 28 '23
You can use a for…of loop.
25
u/Nowaker Dec 28 '23
for...of is functionally and performance equivalent to forEach. Replacing one with another is pointless, and OP shouldn't listen to this nonsense.
2
u/politerate Dec 29 '23
It is pointless im this case, but not when you have async code which you want to await before moving to the next iteration.
8
u/nathan_lesage Dec 28 '23
The difference is in code style. I personally prefer
for (ws of websocketServer.clients) { // do something }to
websocketServer.clients.forEach(ws => { // do something })especially because you have one less pair of brackets to worry about and you don’t wrap everything into a function.
Even if it’s performance wise the same I’d still say reading
for x of someObject.somePropis easier to parse mentally than asomeObject.someProp.forEach7
u/neil3524 Dec 29 '23
Adding to this, using continue, break, and returning from the outer function inside of forEach isn't viable.
For example, if you wanted to only put the try block around ws.send and return in the catch, a for of loop would be required. for that reason, using for of instead of forEach is a no brainer choice
4
u/AgentME Dec 29 '23
Yes, for this you ideally want to use a websocket library that lets you broadcast one message to many connections. Then it will reuse the same message packet data for all of them.
2
u/PrestigiousZombie531 Dec 29 '23
whats the difference between me looping each client and ws broadcasting to all clients, wont it do the same thing internally?
4
u/AgentME Dec 29 '23
Efficiency. Whenever you make a call to send a message to a client, the library has to construct a websocket message out of your data. If you use a websocket library that lets you broadcast one message to many clients, it will only need to create the message packet once. It's the same kind of improvement as moving the JSON.stringify() call out of the loop, but for the message creation steps the library does inside the send function it exposes.
-2
u/Optimal-Builder-2816 Dec 29 '23
This is still fundamentally a for loop to each client, that isn’t really avoidable, nor is it really an issue.
1
u/Psionatix Dec 30 '23
That wasn't the question though.
If you have a for loop that does 5 things and you can decrease the amount of things that for loop does to 4 things by utilising memory and re-using a stored thing instead of re-creating it, you're now doing n-1 less things per loop (where n is the number of iterations for the loop).
The point was efficiency, which is exactly what the comment you replied to was talking about.
2
u/Optimal-Builder-2816 Dec 30 '23
My point was that the term broadcast here is still a for loop, even if it serialize the payload once, you have to notify each client. That’s all I meant.
1
u/Psionatix Dec 30 '23
Yeah good point. They did ask how it was different, so clarifying that it isn’t any different from that perspective is a good addition to the efficiency difference.
2
u/Lazy_Bat8911 Dec 29 '23
You can try to use the publication subscription mechanism to delegate loop and sending to a lower level.
Thus, the code will be executed at the C++ level, which is much faster than Javascript.
If your websocket library does not support it, then I can recommend using uWebSockets.js-40
1
u/Benifactory Dec 29 '23
there’s an id in there though ?
3
u/Optimal-Builder-2816 Dec 29 '23
It’s const.
1
u/ps-ycho Dec 29 '23
Sorry I don’t get it how, can you explain? As per my understanding the req.params can have different value on each api call meaning ids can differ so the object will be different. One way this stringification can be minimized is definitely caching this stringified json using ids. Otherway, I could think of is use a dummy placeholder for the id and use a regex for swapping the placeholder with the id value
1
u/Optimal-Builder-2816 Dec 29 '23
It’s const meant for it won’t change within the scope of that api call so there’s no reason it can’t be serialized outside of the loop the way it’s written here.
1
u/Risc12 Dec 29 '23
The id isn’t from inside the loop, you can move the JSON.stringify to right below line 93.
1
u/ps-ycho Dec 30 '23
Oh didn’t see that for loop. my bad thanks and I was wondering what was I missing
18
u/guppie101 Dec 28 '23
Is this why PUB/SUB is used? For each to send seems like quite a bunch of processing…
4
u/PrestigiousZombie531 Dec 28 '23
using this ws library if it helps how would you write this same function with pub sub
10
u/guppie101 Dec 28 '23
Looks like that library lacks broadcasting. Quite whack. Been using Engine-io, you can use websocket and still broadcast.
8
u/PrestigiousZombie531 Dec 28 '23
what is the difference between doing a foreach vs broadcast, wouldnt the broadcast function internally do the same thing
-32
u/guppie101 Dec 28 '23
GPT can answer it better than I can: Using Redis for pub/sub alongside WebSockets can be advantageous in certain scenarios compared to manually iterating through WebSocket connections using a "for each" approach. Here are some reasons why Redis-based pub/sub can be better:
Scalability: Redis is designed to be highly scalable and can handle a large number of subscribers and publishers efficiently. When you use Redis pub/sub, you can easily scale your application horizontally by adding more instances or servers, and Redis will ensure that messages are distributed correctly.
Decoupling Components: Redis acts as a message broker, decoupling the components of your application. This means that publishers and subscribers don't need to be aware of each other. You can have different parts of your application communicate via Redis without direct dependencies.
Cross-Platform Communication: Redis pub/sub allows different parts of your application, which might run on different platforms or technologies, to communicate seamlessly. You can have Node.js WebSocket servers, Python applications, or any other technology subscribing to and publishing messages through Redis.
Persistence and Durability: Redis can be configured to persist messages to disk, providing a level of message durability. This means that even if a subscriber goes offline temporarily, it can catch up on missed messages when it reconnects.
Load Balancing: Redis supports load balancing and clustering, which can distribute the pub/sub workload efficiently. This ensures that no single server becomes a bottleneck, and the system can handle a high volume of messages and connections.
Reliability and Fault Tolerance: Redis can be configured for high availability and fault tolerance. It supports features like master-slave replication and failover, ensuring that your pub/sub system remains operational even in the face of server failures.
Simplified Code: Using Redis for pub/sub simplifies your WebSocket server code. You don't need to maintain lists of connected clients or manually manage message distribution to clients. Redis handles the messaging infrastructure, allowing you to focus on application logic.
While Redis-based pub/sub offers these advantages, it's essential to choose the right solution based on your specific project requirements. If your application is relatively simple and doesn't require the scalability and flexibility of Redis, a straightforward WebSocket approach may be sufficient. However, for more complex, distributed, or highly scalable applications, Redis-based pub/sub can be a powerful and reliable choice.
20
u/Psychpsyo Dec 28 '23
I think GPT has masterfully dodged the question and instead dumped a bunch of unrelated stuff about Redis, none of which answers what the broadcast functions does internally, if not a for loop.
2
65
u/jhartikainen Dec 28 '23
It's pretty likely to become slow unless you have a fairly beefy machine running it. On an older VPS I had a websocket system with about 100 clients sending some 1000-1500 messages per second. It started having some noticeable latency at that point, but a lot of it was the result of Node's GC.
12
u/PrestigiousZombie531 Dec 28 '23
so if you had to message a million people, what would you do? increase from a 2gb EC2 instance vertically or split across multiple instances horizontally?
19
u/jhartikainen Dec 28 '23
You would need to test it to find out really. It's possible that a bigger instance could run several node instances on the same box if it's not hitting CPU or memory limits. In my case, a lot of it was from GC, so depending on if you hit that also, you could potentially modify your code to generate less garbage.
5
u/PrestigiousZombie531 Dec 28 '23
this thing s currently running on express, i am out of time and have to launch so going with the flow for the time being but back of my mind i am thinking of switching to fastify. What kinda architecture do you think ll work? like can you make a web server that can somehow queue sending messages asynchronously to all connected clients on fastify via websockets?
6
u/Bpofficial Dec 28 '23
If it’s not going to have 10,000 connections straight away it buys you time to improve the implementation. Otherwise Godspeed
0
u/budding_gardener_1 Dec 29 '23
This isn't going to help you right now but if I were you I'd seriously look into moving to NestJS(it's built on express) - at the very least it'll make your code easier to organize as well as making the switch to Fastify down the road a lot easier (if you decide you want to do that)
1
u/PrestigiousZombie531 Dec 29 '23
huge learning curve but thanks for the suggestion
0
u/budding_gardener_1 Dec 29 '23
Hence the "this isn't going to help you right now" at the beginning. But sure, by an means write it off completely.
5
u/PrestigiousZombie531 Dec 28 '23
so what would i do? run apache benchmark on this endpoint while this server runs inside AWS EC2?
14
u/joombar Dec 28 '23
If this is a commercial project, probably the first thing you should do is try to find someone to do the work who understands it
8
u/PrestigiousZombie531 Dec 28 '23
a little too tight on the pocket money department currently so unfortunately i ll have to figure out a way myself
13
u/rowix77 Dec 28 '23
You could use some sort of messaging queue for this task, then distribute it across as many instances as you need. No big deal. Or use a persisted queue in DB, then poll every x seconds, depending on your requirements
1
u/PrestigiousZombie531 Dec 28 '23
lets say we use bullmq for this, where will bullmq run? on a separate express/fastify server?
1
u/rowix77 Dec 29 '23
Yes, for example. You could run it on the same server, however, until it gets slower, depending on usage
5
u/joombar Dec 28 '23
I did say if it’s a commercial product. However, this is the kind of thing that’s not totally easy to get right, it might not be great for what seems like your first node project.
For what it’s worth, consider scaling horizontally to many low powered servers instead of one very powerful one, and making the services cloud-native. If this needs to stay up, you’ll need more than one server anyway, so might as well start from that assumption.
1
u/budding_gardener_1 Dec 29 '23
Move all the state to a message queue and/or database, containerize the app and throw it into fargate.
4
u/jhartikainen Dec 28 '23
Hard to give specifics off the top of my head, it's been a while since I ran any sort of benchmarks on this type of stuff. But yeah you would need to connect 10 000 clients to it and then send messages to them and see what happens.
3
u/ThigleBeagleMingle Dec 29 '23
If you’re already in aws .. move one step further and use apig instead of ec2
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html
6
u/SockPants Dec 28 '23
You will run into (possibly configurable) operating system limits before 1 M users connect to a single instance.
4
u/LetReasonRing Dec 28 '23
If you're distributing messages to millions at once you'll probably be wanting to look at a messaging broker like RabbitMQ, or NATs, or MOSQUITTO.
You can use them all via client libraries and I belive there is websocket functionality for each of them. They'll likely be much more efficient and also allow you to scale much more easily using a variety of potential methods.
7
u/JackAuduin Dec 28 '23 edited Dec 28 '23
Scaling horizontally is how you scale for user counts that high. You could scale vertically with bigger EC2s, but you're going to spend way more money that way.
Not sure what you're using for databases, but you should probably be using an RDS cluster, something that is distributed across multiple regions.
Look into AWS's serverless options like lambda or ECS for your hosting. That way you can set up auto scaling so that you're only using one cluster during low times, then you can scale as each cluster reaches something like 80% CPU or memory. The specific triggers for scaling and how much scaling is done will be specific to your use case.
EC2 is not a great option for this use case.
Finally, the name of it is escaping me right now, but I believe AWS actually has some built-in web socket services. Something where you send an HTTP request to a lambda which then triggers another service that will send millions of web socket messages in an instant for you. Much better for broadcasting things like this. We used it to talk to a fleet of robotic warehouse pallets using a single HTTP request.
Source: AWS Solutions Architect Certification
5
u/Ihavenocluelad Dec 28 '23
Api gateway also has websocket options, not sure if you meant that
1
u/JackAuduin Dec 28 '23
Yes I think you're right, it's been a few months since I was on that project and I admit that I did not implement that myself. But I was part of the discussion! Lol
1
u/Ihavenocluelad Dec 28 '23
Hahah thats the most important part right. I am joining a purely AWS based company next month, very excited
1
u/JackAuduin Dec 29 '23
Highly recommend the solutions architect certifications if you don't have them already. I honestly did it just for the badge on my LinkedIn profile, but it is coming in handy so many times.
1
2
u/naapurisi Dec 28 '23
Remember, that if you have multiple backends horizontally and if they all create websocket servers, the broadcast to other clients would only include those clients that were connected initially to that specific backend. So your load balancer could randomly flow the websocket connection to 1 of N backends. Then if you POST an update to this endpoint the load balancer would randomly flow the request to 1 of N backends and only that backends connected clients would get updated.
Summa summarum, you have a stateful server, when you would probably like to hold state of connected clients inside a Redis database and then any backend app can broadcast the message to all clients.
1
u/intertubeluber Dec 28 '23
I’m not familiar with the AWS offering but azure offers a fully managed serverless websocket pub/sub solution. AWS almost certainly has something similar.
1
Dec 28 '23
If you're on AWS why not use Websicket APIs in API Gateway, then you don't have to worry about scaling and can just use a lambda function to trigger events.
1
u/octipice Dec 29 '23
so if you had to message a million people, what would you do?
Hire both a senior dev and a product manager because you almost definitely do not need to message one million people simultaneously and you need someone who can evaluate both your business case and architecture and come come up with a better solution to your problem.
1
u/--mrperx-- Dec 29 '23
I would use elixir instead of ts, with phoenix live view
use the right tool for the job
0
3
u/ProgrammaticallySale Dec 28 '23
1000-1500 messages per second
There's your problem
2
u/jhartikainen Dec 28 '23
This is just an anecdote of what the performance limits of a Node process are with regards to websockets since OP was asking about it. Not a request to solve it.
1
u/Adybo123 Dec 28 '23
If you’re able to disclose a little bit, what kind of use case were you serving where you had to dispatch 150 messages per client per second? Could those have been batched into larger packets or did latency have to be at ~one ping per 100ms?
1
u/jhartikainen Dec 28 '23
Just a totally random hobby project that had a few users for a little while. Clients were sending individual updates and the system was broadcasting the updates to other clients. Yeah it's entirely possible that it could have been improved by batching them, but it was just something I threw together pretty quick for fun so had no need to optimize :)
0
Dec 29 '23
[deleted]
1
u/jhartikainen Dec 30 '23
Considering the GC pauses in my app were very frequent (like once a second easily), the effect of running a 10 000 message loop has a pretty high likelihood of causing a similar potentially undesirable GC pause. How about you focus on actually giving a useful answer to OP instead of trying to deconstruct someone else's where you don't even know all the details since it sounds like you have knowledge about this.
0
1
u/sambbl Dec 28 '23
Sorry, what is GC?
11
u/jhartikainen Dec 28 '23
Garbage Collection. Node's GC pauses the whole thread for the duration of the sweep, so if you generate a lot of objects (as one might when processing thousands of messages), it can cause latency as a result of the app not responding during the GC sweep.
1
-2
u/davidfally Dec 28 '23
The biggest issue is node’s single thread architecture. Sure you can spin up multiple processes and load balance but orchestrating it will be a PITA. Especially for consistently load balancing websocket connections. On AWS the size of the vps doesn’t really matter, since it’s the same cpu, just more cores (that are going to be unused) you’d need to switch tier instead.
With a beefy cpu (single core performance) and a full 1G - 2.5G uplink node is easily capable to supporting 1000 messages (did this myself in the past)
But in order to reduce latency caused by the single threaded nature I’d suggest an architectural rewrite in something like C#, Java or Elixir
56
Dec 28 '23
Test it
7
u/PrestigiousZombie531 Dec 28 '23
ok but how do I know whats the limit on a 2gb ec2 instance? like how much memory/ram etc does per websocket client object take
49
Dec 28 '23
You can simulate all of this using a tool such as Apache ab https://httpd.apache.org/docs/2.4/programs/ab.html
Push it to breaking point
3
u/KyleG Dec 28 '23
A networking/sysadmin/devops sub would probably be able to answer this question better. I would guess very few people on this sub ever have to deal with 10000 connected websocket clients. But the aforementioned people will have a lot of experience with it.
3
5
u/infinitylord Dec 28 '23
I wouldn't ship this code if 10000 people are going to be using it.
3
u/PrestigiousZombie531 Dec 28 '23
what would you ship then?
7
u/ealmansi Dec 28 '23
The problem with the current solution is that if your userbase grows, it will eventually blow up with any machine size. You may want to consider a redesign.
Do you really need to notify every single client when that endpoint gets called?
Is a certain amount of delay acceptable? If yes, you could keep a buffer per client containing "pending notifications" and send them in batch with an adequate frequency.
If you really want to keep the current design, you can introduce a queue (e.g. bull) such that the request handler merely starts an async job in the background that iterates through all clients (potentially in batches). Or like others have said, you would probably want to use some built-in broadcast mechanism instead of implementing it yourself.
8
u/picklejw_ Dec 28 '23
If your using socketio you can use the built in utilities like broadcast to send to all clients.
6
u/xKiller4Hir3 Dec 28 '23
Not sure why this isn’t higher up. But OP check out socket.io.
Super fun and easy to use. You can even separate clients into separate rooms and broadcast messages to all clients in a room. Or all clients connected to the server. I built a lightweight card game that handles multiple lobbies of 4 players each and can broadcast game updates to each room with the game state stored in redis as a json string.
0
u/PrestigiousZombie531 Dec 28 '23
1
u/flooronthefour Dec 28 '23
You might checkout https://github.com/uNetworking/uWebSockets.js - it's a js wrapper for a c/c++ websocket server that Bun uses
3
Dec 28 '23
Yeah as others have mentioned you will want to setup your architecture to scale horizontally.
If it were me, I would offload these types of “broadcast” (message to all connected clients) to a pubsub queue. Then, the receiver service would be auto scaled and load balanced to handle the traffic.
1
u/PrestigiousZombie531 Dec 29 '23
so in your architecture you have 2 express servers? one that runs the web server and pushes items say to bullmq and another that runs endpoints with only task status and executes these tasks, how will you share websocket server code between both or will you duplicate it?
3
Dec 29 '23
It really depends on what you are trying to do and the amount of traffic. I am personally a big fan of keeping communication logic to clients outside of a main processing API.
What this means is if I, in this short example, would have an API server (assuming your express server), which drops messages to a pubsub queue. This queue would have queue consumer services (other web servers) which would access incoming messages and respond.
So, for example, one pubsub topic broadcasts messages to clients (in bulk). Another sends single messages. These queue consumer services would also be setup to auto scale to handle large amounts of load. Depending on the host (AWS / GCP) there are certain machines and memory sizes which make more sense than others in this situation.
This way you have something which scales and can handle a lot of load. You were asking how to scale to up to 1k to 1m clients.
I handle a system at the moment which delivers push notifications at scale, and many other on-app and email notifications in the hundreds of thousands- millions per month. This is how I have done it.
2
u/lilshoegazecat Dec 28 '23
can i ask you what theme you are using?
1
u/PrestigiousZombie531 Dec 28 '23
tomorrow night blue, i prefer it because it works equally well on day and night times
2
u/anurag_dev Dec 28 '23
Do you really need websockets i.e Is the functionality needed it bidirectional?
2
Dec 28 '23
So will it be called once periodically like every 5mins or will the time gap be more. Is it on the same main monolith or separate instance/pod.
If this runs frequently like every minute and you dint have a beefy system it will most definitely choke. If it runs with more gap the system will still probably choke but will recover. What I mean is calling this will cause a sudden spike and will cause other endpoints to return server error or will cause high latency until all (let’s say 10k) clients have received the message.
At least in my experience that is the behaviour I would expect l.
1
2
u/Sejkom Dec 28 '23 edited Dec 29 '23
Be aware if one of your .send throws, then it potentially will not send it to the rest of the clients.
1
u/PrestigiousZombie531 Dec 29 '23
what circumstances would send throw under and do you think this newer implementation has this problem
2
u/usr_dev Dec 29 '23
Put this in an asynchronous job queue and 2. Use push notifications (web push) instead of web sockets
1
2
u/Aggravating-Agent438 Dec 29 '23
ive an idea, why not mqtt with redis pubsub, u dont need persisted ws
1
u/PrestigiousZombie531 Dec 29 '23
1
u/Aggravating-Agent438 Dec 29 '23
but u can always have another url or subdomain serving ws. just need to ensure to set cors correctly
1
2
u/Likeatr3b Dec 29 '23
Good question OP. I want to ask you about the cost of running your websocket setup. I’m building a websocket messaging system right now and I’m really intimidated by the cost of running real-time dm chat for users.
1
3
u/r-99-l Dec 28 '23
I would suggest to have a few pods running the servers. Like 2-3 and have them communicate with each other. I've built something similar using kubernetes, node js and react.
Maybe try something of this sort if you have time.
3
u/PrestigiousZombie531 Dec 28 '23
i ll have to first of all learn kubernetes, how does it work with ec2. I am guessing EKS would be more complicated to setup compared to say running kubernetes on ec2?
0
u/r-99-l Dec 28 '23
I've not used ec2 nor EKS they'll most likely be similar to azure AKS i guess. Which I have already used and is not really hard to learn, just requires some time.
You might also need some pipelines to deploy to it.
17
u/kYllChain Dec 28 '23
DevOps here, if your application logic is stateless, you have the choice to either scale horizontally of vertically. Both have costs, it's a trade-off to find, you have to do try and error to be pragmatic (there are benchmarks out there but might be overkill for your case).
If you use 100% of your current machine, try to go for next level and see if your app manages to use all that resources. It's likely that your process will reach a maximum where adding resources won't improve your app anymore, that's where you can scale horizontally.
Pricing wise, it's usually the same, on AWS for instance 2 large = 1 xlarge. Usually horizontal scaling is easier to deal with, especially if you use k8s or something alike. You may want to consider horizontal scaling for redundancy point of view as well.
2
u/Kosemani2 Dec 28 '23
There could be performance issues when you write code like this. You can do some load testing with some Nodejs npm package to simulate the outcome. Use map instead and use promise.all instead
2
u/PrestigiousZombie531 Dec 28 '23
is JSON.stringify the bottleneck?
6
u/user345456 Dec 28 '23
Not the bottleneck, but since the string created is the same for each, create it once and use the same string in each call instead of creating 10k strings (in your example of 10k connections).
8
u/Kosemani2 Dec 28 '23
Nope. The bottle neck is the fact that you are looping and possibly doing some blocking activities. The loop will always wait for one to be fulfilled before moving to the next, because you are not performing an asynchronous activity, you are neither using a promise nor an async and await. Even if you are using those you should favour parallelism to concurrency. You may want to read up on those.
3
u/politerate Dec 28 '23
Generally a good point. But if the implementation itself is synchronous, you can't really make them asynchronous right? I guess your idea is to at least decouple them from running after each other, so they can be scheduled separately. This would allow other code to run between them, do I understand it correctly? How would you send these operations in a queue? By promisifying them?
2
u/Horror-Card-3862 Dec 28 '23
nodejs probably isnt best to scale websockets, due to its single threaded nature & difficulty to scale horizontally.
benchmark the capacity of your service, and scale accordingly. The problem with this is that scaling vertically by increasing cpu/ram wouldnt be that efficient because node is single threaded, increasing number of cores doesnt neccesarily mean youre scaling ur application well as node doesnt utilize all cores when handling the websockets.
my advice to move forward would be to try to parelllelize or distribute the websocket load across multiple nodes/cores.
You could use redis as a pub/sub message queue, and have multiple instances of nodes listening to the queue and help you send the messages. This way, you can distribute the load across multiple vms/cores.
check this out: https://eightify.app/summary/web-development/scaling-websockets-with-redis-haproxy-and-node-js-high-availability-chat
I would also suggest before you do anything, try to decouple your websocket to a separate service, so it’s easier to scale your websocket independently from your main app server.
Also, you might wanna consider libraries like socket.io which internally uses uwebsocketjs (a websocket library written in C ported for Javascript). Your performance would likely be better than using websocket/ws. If you want to ship your app fast, use socket.io as it gives you lots of apis to use, rather than reinventing the wheel.
You could also consider Golang for websockets as they can handle sockets much more efficiently.
1
5
u/casualPlayerThink Dec 28 '23
Autocannon it, then you will see :) Just don't forget to tell the test to the host and devops.
In fact, most likely you will have timeouts, refused connections, extreme load and lots of lots of throttling. If it is an auto scaling cloud provider based stuff, then expect high receipt at end of the month :)
1
u/Imperial_Swine Dec 28 '23 edited Dec 28 '23
Disclaimer, not a node developer professionally.
I think you should wrap sending the ws message in a promise and make sure you do an async operation for pinging all your clients.
A synchronous for each will take forever.
Furthermore you'd need to scale your infrastructure up appropriately if the server is facing difficulties.
EDIT: Ignore please, forgot about how threading works with Node JS
1
u/PrestigiousZombie531 Dec 28 '23
3
u/politerate Dec 28 '23
Why not send them all in parallel:
const payload = JSON.stringify({ action: 'news/latest/onWebSocketNewsItemUpdate', data: id, type: 'INSERT', }); const sendPromises = []; websocketServer.clients.forEach((ws) => { const sendPromise = new Promise((resolve, reject) => { ws.send(payload, (error) => { if (error) { reject(error); } else { resolve(); } }); }); sendPromises.push(sendPromise); }); await Promise.all(sendPromises); res.locals.data = true; return next();2
u/Horror-Card-3862 Dec 28 '23
foreach executes ws.send asyncrhonously too, as ws.send is async, it doesnt wait for each iteration to complete before continuing to the next iteration.
2
u/politerate Dec 28 '23 edited Dec 28 '23
Are you sure ws.send is async, I assumed it's sync. If it's async forEach won't wait for the promise to resolve so doesn't really block anything.
Edit 1: yeah I think ws.send is all non blocking
Edit2: it seems ws is all fire and forget, without waiting for any feedback
Edit3: So I think it's not really async in that you will get a promise which will eventually resolve, but it's also non blocking, since it doesn't wait for any feedback. Theoretically if you would have a reaaaaaally long list of clients the operation of send is quick but it would still be sync-ish in nature (even if non blocking, since the buffer sending will be non blocking). This means that those million sends will run sequentially, i.e. won't allow other code to run in between if run with forEach.
Summa summarum: In practice it will most likely be no problem even using forEach
3
u/tbranyen Dec 28 '23
Yea this looks like pure overhead in a hot code path. I would avoid wrapping promises unnecessarily.
1
u/PrestigiousZombie531 Dec 29 '23
thank you for sharing some code, what happens if one of the ws send fails, will it fail the rest since we are doing promise.all?
2
u/politerate Dec 29 '23
After some thought I think this will not improve anything, as discussed further in this thread, it might even make the whole thing slower. As for your question, Promise.all will reject immediately if something fails, so if you want to wait for all promises to complete (failed or not) you would use Promise.allSettled
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
1
u/PrestigiousZombie531 Dec 29 '23
I made a promise free version as it doesnt seem ws.send needs promises, what do ya think? What happens here if ws.send fails for one client?
2
u/politerate Dec 29 '23
I have no experience with websockets, but this seems fine to me. If it fails for one client you log it and that's it (with the code you provided)
9
u/MaxUumen Dec 28 '23
There's nothing in that code to be improved with Promises. NodeJS can't run JS code in parallel (without going too deep for what it's worth here and understandable to you).
2
2
u/politerate Dec 28 '23
The only thing which I am not sure right now but could make minimal difference is the following imagined scenario: As I understand the ws.send is non blocking in itself. So it will run fast at least, even if buffers are being transported in the background. Now let's say you have 100k clients. By using forEach, it will now loop through those 100k without allowing other code to run in between. Does promisifying them give more room for execution of immediate following sync code? At the end of the day the execution of .send() will be delayed but will give immediately following sync code the chance to run before them.
2
u/MaxUumen Dec 29 '23
It would only make it all slower. In the end, the amount of useful work will not change, but you would be adding extra work by introducing the Promises. The work that can be parallelized, is already running in other threads outside of JS anyway (networking and other IO).
Yes, you could theoretically run multiple of those loops in "parallel" by splitting to chunks and alternating which one gets to run, but it would not magically make anything faster.
Of course you could also use child processes, but that's extra code and because eventually multiple instances are needed anyway, I wouldn't waste time on that additional code complexity. And it's not even granted it would actually perform better as a whole.
2
u/politerate Dec 29 '23 edited Dec 29 '23
Without introducing worker threads and the like, I feel the forEach logic can't be improved further. You are absolutely right, promisifying would probably just add tons of overhead. Ok I found the exact issue I am talking about: https://github.com/websockets/ws/issues/617#issuecomment-324935706
Edit1: Also interesting: https://github.com/websockets/ws/issues/617#issuecomment-393396339
2
u/forloopy Dec 28 '23
Yeah there’s nothing being waited on it’ll all run exactly the same. Only way promises make it faster is if there’s a different implementation of .send that itself is asynchronous- assuming it already isn’t already an unhandled promise
0
u/byutifu Dec 28 '23
Yeah, 1000 should be fine if you parallelize. I do this by throwing everything in a map and running promise.all on the map. If you need more, you could chunk by 500, parallelize, next batch
1
u/Adybo123 Dec 28 '23
This does nothing except slow your code down unless you’re waiting on callbacks from native modules that perform Async I/O.
All you’re doing is adding overhead from the time required to create and resolve 500 promises. Node doesn’t execute multiple lines of JavaScript at once.
1
u/byutifu Jan 01 '24
Well, how are you gonna get all the calls then? Just not do it? Promise.all is definitely faster than 1x1…. Idk what you’re implying
1
u/Adybo123 Jan 05 '24
Please see my example code and measurements here: https://gist.github.com/adamsoutar/b75aab816b814fdf337582742a821cf2
For a simple workload of stringifying an object,
Promise.allis 11 times slower than 1x1 for 200 iterations.
3
u/hydraulix989 Dec 29 '23 edited Dec 29 '23
PubSub would be better for this, something like Pusher. If you have to have your own backend, use something with Golang channels and have sharding.
4
u/FatDistribution Dec 28 '23
If you’re doing a push from sever to clients and not bidirectional, server sent events might be a good option too.
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Here is an example with NestJs (express based)
2
2
Dec 29 '23
Side note, 10,000 websocket connections on the one node like this is a bad idea. Server-Sent-Events across multiple nodes are a much better approach in my experience, having used both in large systems. SSE's just play nicer with other resources (firewalls, app gateways, logging, monitoring), and performance is fine.
2
u/ncubez Dec 28 '23
Dockerize that shit and run it on kubernetes and set the deployment's replicas to a sufficient number.
1
u/SockPants Dec 28 '23
What happens if one send fails? What happens if the original REST request times out before all the messages are sent?
1
1
1
u/joshtronic Dec 29 '23
Premature optimization unless you have that sort of traffic in production already
1
u/foobarbazquix Dec 29 '23
Sounds like you have the need for a true pub sub architecture. If that’s the case I’d recommend not avoiding it. If you really need to broadcast to 1,000,000 over websocket as close to the speed of light as possible then you will need more infrastructure and different architecture capable of delivering that. If you’re pressed for time and ok burning some cash for less than stellar results you could scale what you have verticallly as a stopgap. Really you need to be going horizontally.
I think you said you’re on AWS. I’d recommend AWS IoT over AWS API Gateway websockets. With the latter there is no broadcast, so you’d be doing the for each in a lambda still. AWS IoT is basically infiniscale MQTT — pub sub messaging. It’s branded for internet of things but you can use it for anything, including what you want to do — broadcast to websocket clients. You’ll want to look for MQTT over websockets.
The good news is your server code is simpler than alternatives because you just write to a topic using the AWS IoT client. Alternatives at the scale you’re talking about would probably involve sharding and stuff. Caveats include message size: limit is either 128k or 256k I think. If you need larger than that you would probably be solving it just like any other size limit—AWS IoT or mqtt doesn’t offer anything unique that would help or anything that I’m aware of.
Your client code needs to subscribe to those topics. There’s a way to do this securely in the browser with AWS IoT but it’s some work. If the client isn’t in a browser it’s a bit easier to connect via websocket or even just mqtt over tcp you wanted.
In either case your clients subscribe to topics and they get all messages that are published to them while they’re connected. You can subscribe to fancy wildcard topics and stuff too. Not the easiest to set up but not the hardest either and well worth it to have a legitimate pub sub setup if you see yourself doing this for a while. It will scale to whatever you need. Last I checked the cost is reasonable if not dirt cheap.
Scaling and perf are good problems to have. I would want to make absolutely sure I had them before dedicating a lot of time into solitons for them that could be better spent on something else.
Good luck.
1
1
u/Sorry-Turn-5468 Jan 01 '24
Why not have all users connected to a session server using long polling? When someone broadcasts a message, it goes to Kafka. One server will consume the message to save it to the database. Another consumer will send the message to online users, and one more will write the new messages for offline users. You can distribute your session servers and scale them up.
1
-8
0
Dec 29 '23
[deleted]
1
u/PrestigiousZombie531 Dec 29 '23
i see a lot of people talking about pub sub, can you please elaborate what exactly are you doing? like do you have 3 applications? 1 of them runs express but subscribed to redis streams, one of them is a separate worker that pubishes? or did i get that all wrong?
88
u/codeedog Dec 28 '23 edited Dec 28 '23
Every answer is focused on the tech you’re using and making recommendations to scale it faster with other tech. The real questions are:
what is your system architecture?
Why is every client being notified immediately?
Do they need to be notified immediately or can the notification be delayed?
How often are these messages broadcast?
What are you trying to do?
To properly scale a system, you’ll need to make trade offs. You’ll be trading some efficiency for more backend complexity, etc. Without more details, it’s difficult to answer the real question.
For example, if I could avoid notifying 10K connects clients immediately, I’d definitely delay that. Most may not even care about the update. Or, maybe by the time they care, it’s been overwritten 20 times.
Without knowing what you’re trying to do, all everyone is doing is helping you go faster towards a geometric breakpoint.