Design a chat server

Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest problems to solve?

Lets try to start with analyzing the requirements, what would we need for this chat server?

1.We need a server that coordinate the chat activity. The server need to be running at all times.

2.The server need to support a small (large) number of clients.

3.The server need to maintain a list of the clients, each clients will have a status showing it is online, offline or away.

4. Each client will have a contact list that list all his/her contact. lets say that the contact must be mutual, which means if client A is a contact of client B, then client B must also be a contact of client A.

5. Lets keep things simple, only two clients that are in each other’s list and both of them are online can send messages to each other. The message could be send in XML format which is good for both human and machine to read.

Then lets try a typical user case see if we have everything covered. Here are the steps in this typical user case
1. Client A signs online, changes his/her status.
2. Client B signs online, changes his/her status.
3. Client A send a request to B asking to be added to B’s contact list.
4. Client B approve the request, now A and B are in each other’s contact list.
5. Client A and B send each other messages, messages delivered.
6. Client B left the chat and sign off status of client B change.
7. Client A sign off.

Here are some classes that will be needed in our application.

enum Status // 2, or 3 status.
class User // which will contains the contact list as well as the request.
class Request // request from one user to another to be each other's contact
class Server // server class that runs on the server and perform clients register, and coordination of the different client

Here are some problem that we might need to consider.
1. How would we know that a client is online? I mean really really online.
The user connection might be dead for some reason, so we might want to pin the user periodically if they are online.

2. What if there are conflict between the data stored in the client machine and the server machine?
If the data are out of sync, we need to have a mechanism to decide which one is right. For example, the status we should probably sync on the local machine while the contact list we might want to sync on the server.

3. What if there are many users, say millions of them?
We might want to use distribute the application in many servers, and syncing will be a problem.

4. How to prevent the DOS attacks, we need to think about the limit the frequency that each user can send messages or request to server.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s