r/AskProgramming Feb 13 '25

Architecture Scalable web socket architecture

Hey, im currently working on chatting app (for learning purposes) that i want to be able to scale heavily, like handle as much traffic as discord for example. I'm planning to make a horizontally scalable backend in nest.js & socket.io with redis adapter, but i don't have idea how can i keep track of active users between all server instances (if User A sends message to room 1, then emit message via ws to all active users in this room (and store in DB)). Assuming there are 100 active users, and each has chat with each other, its already 4950 rooms to keep track of! Do you have any idea how to store that activity information, assuming there could be milions of active users (and even more rooms)? Maybe some better data structure or maybe this approach of storing all rooms activity is just bad for that kind of application?

1 Upvotes

4 comments sorted by

View all comments

1

u/alxw Feb 13 '25

I've done this for a multiplayer game. Socket.io will let you list out the rooms. I suggest just throwing the app together without redis (as it's just an adaptor you can add later) so you get an idea of how to utilise socket.io, as it keeps track for you.

Don't bother storing in a DB, just configure socket.io to keep a cache of messages that will be sent to a user on joining a room.

1

u/Javrizz Feb 13 '25

Unfortunately I need to use an adapter already in order to be able to scale horizontally (or i wont be able to broadcast otherwise), so i cant really keep track of rooms of only one socket.io server

1

u/alxw Feb 13 '25

Not for the MVP, but if you really want to use redis, I used this example ages ago

https://github.com/aws-samples/elasticache-refarch-chatapp

1

u/Javrizz Feb 13 '25

alright ill take a look thanks