ChatApp is a real-time chat application built using a microservices architecture with Node.js. The project uses WebSocket for bidirectional communication between clients and servers, and each service is designed to handle specific tasks within the chat ecosystem.
- Project Overview
- Features
- Microservices Architecture
- Setup and Installation
- API Documentation
- Design Patterns and Refactoring
- Future Enhancements
ChatApp is a real-time chat application where users can:
- Sign up, log in, and manage their profiles
- Send and receive messages instantly
- Get notifications for new messages
- View online/offline status of other users
This project is implemented using a microservices architecture to handle the different responsibilities of a chat app. WebSocket is the main protocol used for real-time chat, while HTTP is used for other operations such as user authentication and notifications.
-
User Management:
- Signup, Login, and Profile management using stateless API requests.
- Secure login and user authentication.
-
Real-time Messaging:
- Bi-directional communication between clients and servers using WebSocket.
- Persistent connections for continuous chat sessions.
-
Notifications:
- Push notifications for new messages, even when users are offline.
-
Presence Status:
- Real-time updates on users' online/offline status.
- Allows users to know when their contacts are available.
-
Chat History:
- Chat history storage in a key-value store to retrieve previous messages when users log back in.
The ChatApp backend consists of the following microservices:
-
API Server:
- Manages user registration, login, and profile-related operations.
- Exposes HTTP endpoints for CRUD operations on user data.
- Routes requests to appropriate services and performs authentication.
-
Chat Server:
- Handles all chat-related WebSocket connections.
- Facilitates real-time messaging between users.
- Broadcasts messages to connected clients and manages message delivery.
- Stores chat history in a key-value store for quick access when users reconnect.
-
Notification Server:
- Integrates with third-party notification services to push alerts to users.
- Sends notifications when users receive new messages or other alerts.
-
Presence Server:
- Tracks users' online/offline statuses.
- Notifies other users when their contacts' statuses change.
- Manages WebSocket connections to detect presence status in real-time.
- Node.js (v18 or higher)
- npm or yarn
- Redis (for presence tracking and chat history storage)
-
Clone the Repository:
git clone https://github.com/jawherKl/chat-app.git cd chat-app
-
Install Dependencies for Each Service: Navigate to each service directory and install dependencies.
cd api-server npm install cd ../chat-server npm install cd ../notification-server npm install cd ../presence-server npm install
-
Start Each Microservice: Run each service in a separate terminal window.
# API Server cd api-server node index.js # Chat Server cd ../chat-server node index.js # Notification Server cd ../notification-server node index.js # Presence Server cd ../presence-server node index.js
-
Testing:
- Use Postman or
curl
commands to test HTTP endpoints (e.g.,/api/signup
,/api/login
). - Use WebSocket testing tools (e.g.,
wscat
or Postman) to test WebSocket connections on the chat and presence servers.
- Use Postman or
- POST /api/signup - Registers a new user.
- POST /api/login - Authenticates a user and returns a token.
- GET /api/profile - Retrieves the profile of the logged-in user.
- WebSocket Endpoint -
/chat
- Allows users to send and receive real-time messages.
- POST /notify - Sends a notification to a user about a new message or other events.
- WebSocket Endpoint -
/presence
- Tracks and updates users’ online/offline status.
The ChatApp project incorporates the following design patterns for improved code maintainability and scalability:
-
Factory Pattern:
- Used to create instances of WebSocket clients and notification handlers dynamically.
-
Observer Pattern:
- Used for the presence service to notify clients about the status changes (online/offline) of other users.
-
Singleton Pattern:
- Ensures only one instance of services like the Redis connection pool and WebSocket server.
-
Service Registry:
- Used to manage service discovery. The registry provides the client with the appropriate chat or presence server based on the current load.
-
Router Pattern:
- Routes incoming HTTP requests in the API server to specific microservices based on the endpoint.
Some possible enhancements include:
- Enhanced Notification Handling: Integrate with more notification channels for flexible alerts.
- Load Balancing: Add load balancers to distribute the workload across instances.
- Message Encryption: Ensure message data security by adding end-to-end encryption.
- Scalable Key-Value Storage: Use distributed databases to store chat history and ensure scalability.