Design a Chat Application
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-08-29
Problem Statement
Designing a real-time chat application that allows users to send and receive messages individually or in groups. Focus is on object-oriented design, modularity, message delivery, and persistence.
✅ Functional Requirements
1.User Management
•Register, login, and logout users.
•Maintain user presence status: online, offline, away.
2.Messaging
•One-to-One Messaging: Send messages directly to another user.
•Group Messaging: Send messages to a group of users.
•Messages should have: messageId, senderId, receiverId/groupId, content, timestamp.
3.Message Delivery Guarantees
•Ensure at least once delivery.
•Maintain message order.
•Optionally, track read receipts.
4.Chat History & Persistence
•Store messages in a database for history retrieval.
•Support loading previous messages when a user opens a chat.
5.Notifications
•Notify users of new messages if offline.
•Optional: Push notifications on mobile devices.
6.Optional Features
•Message editing and deletion.
•Media attachments (images, files).
•Typing indicators.
🔐 Constraints & Assumptions
•Focus on LLD, class design, and modular components, not on front-end UI.
•Assume network communication is handled via WebSockets or similar.
•System should support multiple concurrent users.
🧱 Key Components & Classes
1.User Class
1
2class User {
3private:
4 string userId;
5 string username;
6 string status; // ONLINE, OFFLINE, AWAY
7public:
8 void setStatus(string status);
9 string getStatus();
10};1.Message Class
1
2class Message {
3private:
4 string messageId;
5 string senderId;
6 string receiverId; // userId or groupId
7 string content;
8 DateTime timestamp;
9public:
10 void send();
11 void edit(string newContent);
12};1.ChatRoom / Group Class
1
2class ChatRoom {
3private:
4 string roomId;
5 vector<User*> participants;
6 vector<Message*> messages;
7public:
8 void addMessage(Message* message);
9 void addParticipant(User* user);
10 void removeParticipant(User* user);
11};1.MessageService Class
•Handles sending, receiving, storing, and retrieving messages.
1
2class MessageService {
3public:
4 void sendMessage(Message* message);
5 vector<Message*> getMessageHistory(string chatId, int limit);
6};1.NotificationManager Class
•Sends notifications to offline users.
1
2class NotificationManager {
3public:
4 void notifyUser(User* user, Message* message);
5};1.ConnectionManager Class
•Manages active user connections and presence status.
🔄 Example Workflow
1.User A sends a message to User B → MessageService.sendMessage().
2.Message stored in database → ChatRoom.addMessage().
3.If User B is online → deliver message in real-time.
4.If User B is offline → NotificationManager.notifyUser() triggers notification.
5.User B reads the message → optionally update read receipts.
🧪 Expected Deliverables
•Class diagram showing User, Message, ChatRoom, MessageService, NotificationManager, and ConnectionManager.
•Implementation of message sending, storage, delivery, and notifications.
•Test cases:
•One-to-one messaging
•Group messaging
•Offline message delivery
•Message history retrieval
•Concurrent message sending
💡 Discussion Points
•Scalability: handling thousands of concurrent users.
•Concurrency: thread-safe message delivery and storage.
•Extensibility: supporting media messages, reactions, and typing indicators.
•Persistence: efficient storage and retrieval of chat history.