A real-time backend chat application built with Node.js, WebSocket (Socket.IO), JWT authentication, and MongoDB.
This is the backend for the real-time chat application:
chat-app-frontend
- ✅ JWT-based user authentication
- 🔌 Real-time communication with Socket.IO
- 🧠 MongoDB for storing users and messages
- ✨ Built with Javascript
- Node.js
- Express.js
- MongoDB + Mongoose
- Socket.IO
- JWT
chat-app-backend/
├── src/
│ ├── config/ # DB and environment config
│ ├── controllers/ # RESTful logic
│ ├── models/ # Mongoose schemas
│ ├── routes/ # Express route handlers
│ ├── sockets/ # WebSocket events
│ └── index.ts # Entry point
├── .env.example # Sample environment variables
└── package.json
git clone https://github.com/MatheusIshiyama/chat-app-backend.git
cd chat-app-backend
npm install
# or
yarn
Copy .env.example
to .env
and configure:
PORT=4000
MONGO_URI=mongodb://localhost:27017/chatapp
JWT_SECRET=your_jwt_secret
Development mode:
npm run dev
Production build:
npm run build
npm start
Method | Endpoint | Description |
---|---|---|
POST | /auth |
Authenticate and return token |
GET | /user |
Get logged-in user data |
POST | /user/register |
Create a new user |
GET | /user/confirm |
Validate email from new user |
Protected routes require:
Authorization: Bearer <token>
Event Name | Direction | Description |
---|---|---|
sendFriendRequest |
Client → Server | Send a friend request to a user by username |
declineFriendRequest |
Client → Server | Decline a received friend request |
acceptFriend |
Client → Server | Accept a received friend request |
chat |
Client → Server | Request to open or create a chat with a user by username |
newMessage |
Client → Server | Send a new message in an existing chat |
chatId |
Server → Client | Response with chat ID and friend name when chat is triggered |
chatMessages |
Server → Client | Regularly emits full message list of the current chat |
chatLastMessages |
Server → Client | Periodically sends a summary of last messages from all chats |
friendsRequests |
Server → Client | Periodically sends list of friend requests |
friendList |
Server → Client | Periodically sends current list of friends |
alert |
Server → Client | Emits alert messages like "user not found" or "error to add yourself" |
Licensed under the MIT License.