I really love the design of socket.io and I was planning on making a matchmaking system using it. Everything I've done so far has worked so far. However, I worry about scalability, what happens if I start having thousands of lobbies, with thousands of players connecting to it? How can I ensure that socket.io will scale up to millions of connections, or at least hundreds of thousands?
-
it does scale, check https://stackoverflow.com/questions/38701647/scalable-architecture-for-socket-io. worry about scale later when you have actual users. http://paulgraham.com/ds.html – deadcoder0904 May 23 '20 at 05:07
-
It doesn't scale as well as http because it uses a persistent socket connection and there's both a theoretical limit of number of sockets and practical OS limits. The theoretical limits is not worth considering because it is 65 thousand times more than number of possible IP addresses - and with IPv6 that's a very large number. What you are limited by is number of open file descriptors your OS supports: https://stackoverflow.com/questions/2332741/what-is-the-theoretical-maximum-number-of-open-tcp-connections-that-a-modern-lin – slebetman May 23 '20 at 05:31
-
.. but this is a limitation faced by a lot of very successful products/protocols: VPN (except those that use UDP), SSH, Skype, FTP, Bittorrent, various MMORPG etc. The reason it works is because humans cannot hold conversations with more than a few dozen people at a time. As you approach 100 people in a chat room it starts to feel like a class. As you approach 1000 it doesn't feel like a chat room. Also, you can easily scale by adding more servers and isolate chats by server - just like how MMORPG games work – slebetman May 23 '20 at 05:36
2 Answers
you can use UWebSocket.js for better performance and large scale program
but you need to implement your communication protocol like socket.io, and you can implement it like this example (don't use this in production this is just a toy example)
const Event = require('events')
class EventRouter extends Event {
async process (msg) {
this.emit(msg.event, msg.data)
}
}
let router = new EventRouter()
// set up your connection and sockets
socket.on('message', router.process)
router.on('your route & your event ....', msg => {
// your handler
})
function emit(socket, event, data) {
socket.send({event, data})
}
and repeat this code for your client-side

- 16
- 1
- 3
Does socket.io Scale? Sort of...
Socket.io its self is about solving some of the thorny issues in managing sockets and fallbacks. its great, and will get you up and running. However its not really socket.io you need to worry about with scale. It just manages the sockets. at most its a bit less scalable than doing raw, but it's never the whole solution. You'll normally run out of CPU or TCP connections first. This means you end up needing to go broad, and have more than one machine doing things at once.
You'll need more than just Socket.io to get to millions of connections. The most popular answer is using redis to handle the distribution and using something like kubernetes to handle the horizontal scale of frontend servers.
In my experience a lot of people seem to have issues getting above 30k connections (I work for Ably.io) but it is doable...
It is a lot of work to manage all of these bits, and unless its actually core to your offering to own this tech, why not use AWS API gateway websockets or a hosted provider of sockets/pubsub?

- 66
- 4
-
could you please take a look at my question https://stackoverflow.com/questions/73757107/socket-io-scaling-issue – Sujith S Manjavana Sep 18 '22 at 05:08