Reputation: 21
I am new to socket.io in nodejs . i had a doubt how many concurrent connection can i connect with my nodejs app and is it possible to connect 100K user concurrently . my code is working fine
my code
io.on('connection', (socket) => {
socket.on('joinroom', ({ username, room }) => {
const user = userjoin(socket.id, username, room);
socket.join(user.room);
socket.emit('message', formatmessage('bot', 'Welcoming by bot'));
socket.broadcast.to(user.room).emit('message',formatmessage(' bot',`${user.name} just joined`));
});
socket.on('chatMessage', (msg) => {
const user = getCurrntuser(socket.id);
io.to(user.room).emit('message',formatmessage(user.name,msg));
});
socket.on('disconnect',()=>{
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit('message',formatmessage('bot',`${user.name} just now disconnected`));
}
});
});
Anyone had idea how any connection is possible at same time ! thanks for your help
Upvotes: 1
Views: 2440
Reputation: 778
Microservices architecture solves scalability problems like these. These are problems you would have not only in NodeJS but in other languages as well. I believe that the solution in your case is to work with a load balancer scaling your service. take a look at https://pm2.keymetrics.io/docs/usage/quick-start/
Upvotes: 0
Reputation: 2531
The question is hard to answer because it depends on the machine that runs the code and the intensity of the communication.
But I would like to address that the question should be more akin to "How to support a large number of clients using NodeJS" because today it's 100k tomorrow 10k or 1M.
You should look into microservice architected and scaling solutions. The basic gist is you need the ability to add more replicas of the same stateless service when you need to support more users/connections. This can be achieved when using a load balancer and stateless microservice.
I know this isn't a direct answer to the question but I hope it will put you on the right path to solve the issue.
Upvotes: 3