The Chat API is a backend service for a real-time chat application that supports user registration, authentication, one-on-one messaging, and group chat functionality. It leverages Node.js, Express.js, Socket.IO, and MongoDB to provide real-time communication between users, while ensuring secure data handling through JWT-based authentication.
- 🔐 Secure User Authentication – JWT-based token system keeps your data safe.
- 👥 Friend Management – Add and remove friends with ease.
- 💬 Real-Time Messaging – Send and receive messages instantly using WebSockets.
- 🧑🤝🧑 Group Chat – Create group chats and enjoy dynamic conversations.
- Node.js – The engine that powers the API logic.
- Express.js – Framework for building the RESTful endpoints.
- MongoDB – NoSQL database for storing users and chats.
- Socket.IO – Real-time communication with WebSockets.
- Mongoose – ODM for MongoDB.
- JWT – Authentication with JSON Web Tokens.
- bcrypt – Password hashing for security.
Before you begin, make sure you have:
- Node.js (v14+ recommended)
- MongoDB (Local or hosted on MongoDB Atlas)
-
Clone the repository:
git clone https://github.com/glovic/Chat-API.git cd Chat-API
-
Install dependencies:
npm install
-
Configure your environment: Create a
.env
file with the following values:MONGO_URI=mongodb://localhost:27017/chat-db JWT_SECRET=your_jwt_secret PORT=3000
-
Run the server:
npm start
-
The API will be up and running at
http://localhost:5000
! 🎉
-
Signup –
POST /signup
- Create a new user:
{ "username": "john_doe", "email": "john@example.com", "password": "password123" }
- Create a new user:
-
Login –
POST /login
- Receive a JWT token for further requests:
{ "email": "john@example.com", "password": "password123" }
- Receive a JWT token for further requests:
-
Add Friend –
POST /friends/add
- Add a friend using their user ID.
{ "friendId": "userId123" }
- Add a friend using their user ID.
-
Remove Friend –
POST /friends/remove
- Remove a friend using their user ID.
{ "friendId": "userId123" }
- Remove a friend using their user ID.
-
Send Message (Socket.IO event)
- Send a message to a friend in real time.
{ "from": "userId123", "to": "userId456", "message": "Hello!" }
- Send a message to a friend in real time.
-
Receive Message (Socket.IO event)
- Listen for incoming messages in real time.
-
Create Group –
POST /api/groups/create
- Create a new group chat:
{ "name": "Cool Group", "members": ["userId123", "userId456"] }
- Create a new group chat:
-
Send Group Message (Socket.IO event)
- Send a message to a group:
{ "groupId": "groupId123", "message": "Hey team!" }
- Send a message to a group:
This API leverages Socket.IO to facilitate instant messaging. Here’s how you can test the real-time features:
-
Connect to WebSocket:
- Use a WebSocket client like Socket.IO Client or WebSocket King.
- Connect to
ws://localhost:3000
.
-
Send Message:
- Use the
send_message
event to send messages between users.
- Use the
-
Receive Message:
- Listen for the
receive_message
event to receive real-time messages.
- Listen for the
Here’s an overview of the directory structure:
/chat-api
├── config # Environment and database config
├── controllers # Business logic (auth, user, chat)
├── models # MongoDB models (User, Message, Group)
├── routes # API endpoints (auth, users, messages, groups)
├── websockets # WebSocket logic for real-time chat
├── middleware # Authentication middleware (JWT)
└── server.js # Main entry point for the server
Here are a few enhancements planned for future versions of the Chat API:
- Add message history retrieval.
- Implement read receipts and typing indicators.
- Improve error handling and validation.
- Expand testing coverage with unit and integration tests.