In examples, I saw of socket programming projects (most of which were chat projects), they often saved all the clients in one array, and when a message was received from a client, in addition to saving it in the database, to all clients also was sent.
The question that comes to my mind is: How can this message received from the client and saved in the database and send to clients when number of clients is very large? (I mean, the number of customers is so large that a single server can’t meet their demand alone, and several servers are needed to connect sockets).
In this case, not all clients can be managed through the array. So how do you transfer a message that is now stored on another server (by another customer) to a customer on this server? (Speed ββis important).
Is there a way to quickly become aware of database changes and provide them to the customer? (For example, Telegram.)
I’m looking for a perspective, not a code.
2
Answers
Start by reading about the C10K problem and then continue with the C10M problem.
You should use your database as your messaging center. Have other servers watch for changes in the database either by subscription or by pulling on a time interval. Obviously subscription would be fastest possible.
When a message is inserted, each server picks this up and sends to their list of clients. This should be quite fast for broadcasting messages.