This is the server-side of a real-time chat application with file-sharing capabilities. Built using Node.js, Express, Socket.IO, and MongoDB, it allows users to join chat rooms, send/receive real-time messages, and share files using Node.js streams and buffers. User authentication, chat history, and file metadata are stored securely in MongoDB.
- Real-time Communication: Users can send and receive messages in real-time using Socket.IO.
- File Sharing: Efficient file uploads/downloads with Node.js streams and buffers.
- User Authentication: Secure user login and registration using JWT (JSON Web Tokens).
- Persistent Chat History: MongoDB stores chat messages, user data, and file metadata for future retrieval.
- Scalability: The application is designed for horizontal scaling and optimized for concurrent users.
- Node.js: JavaScript runtime for building server-side logic.
- Express: Fast web framework for Node.js.
- Socket.IO: Enables real-time, bidirectional communication between server and client.
- MongoDB: NoSQL database used for chat storage and user management.
- JWT: Secure user authentication using JSON Web Tokens.
- Streams & Buffers: Handle large file uploads/downloads without blocking the server.
.
├── .env
├── .env.example
├── .gitignore
├── app.js
├── config
│ ├── db.js
│ └── jwtConfig.js
├── controllers
│ ├── authController.js
│ ├── chatController.js
│ └── chatRoomController.js
├── middleware
│ └── authMiddleware.js
├── models
│ ├── ChatMessage.js
│ ├── ChatRoom.js
│ └── User.js
├── routes
│ ├── authRoutes.js
│ ├── chatRoomRoutes.js
│ ├── chatRoutes.js
│
├── services
│ └── socketService.js
├── socket.js
├── swagger.js
├── uploads
├── utils
│ ├── logger.js
│ └── responseFormatter.js
├── seeder.js
└── tests
├── auth.test.js
└── chat.test.js
- Node.js (version 14 or above)
- MongoDB (locally or cloud-hosted)
- Docker (optional, for containerized deployment)
-
Clone the repository:
git clone https://github.com/jmrashed/chat-backend.git cd chat-backend
-
Install dependencies:
npm install
-
Create a
.env
file:In the root of the project directory, create a
.env
file with the following content:PORT=3000 MONGODB_URI=mongodb://localhost:27017/realtime-chat-app JWT_SECRET=your_secret_key
-
Start MongoDB:
Make sure MongoDB is running locally or connect to your cloud MongoDB instance.
mongod
-
Run the server:
Start the application with:
npm start
-
Access the Application:
Open your browser and navigate to
http://localhost:3000
.
To execute tests for authentication and chat functionality, use the following command:
npm test
To run the server in a Docker container:
-
Build the Docker image:
docker build -t realtime-chat-app-server .
-
Run the Docker container:
docker run -d -p 3000:3000 realtime-chat-app-server
-
Access the Application:
Navigate to
http://localhost:3000
to use the chat application.
For API documentation, you can explore the endpoints through Postman or Swagger:
- Postman Documentation: Postman Collection
- Swagger UI: Available locally at
/api-docs
after starting the server.
- Horizontal Scaling: Cluster the app or use multiple instances behind a load balancer.
- Redis: Use Redis to handle session management and WebSocket scaling with Socket.IO.
- Database Indexing: Ensure MongoDB collections are indexed for optimal query performance.
- Rate Limiting: Implement rate limiting to control API usage and prevent server overload.
The application includes robust error handling for:
- Invalid JWT tokens
- Database connection errors
- File upload/download issues
- User authentication failures
- Private Messaging: Implement direct messaging between individual users.
- Group Chats: Allow for group chat rooms with multiple participants.
- Notifications: Add real-time notifications for new messages or file uploads.
- OAuth Integration: Implement social logins via Google, Facebook, etc.
- Admin Dashboard: Build a dashboard for managing users, chat rooms, and file uploads.
This project is licensed under the MIT License.
Here are some screenshots of the project: