API de chat en tiempo real con WebSockets (Socket.io), salas públicas/privadas y autenticación JWT.
Stack: Node.js · Express · TypeScript · MongoDB · Socket.io · Docker
Portfolio project — REST + WebSockets, membresía de salas y presencia de usuarios.
- Auth JWT (register / login / me)
- Mensajería en tiempo real (crear, editar, eliminar)
- Salas públicas y privadas + join/leave
- Estado de usuario: online / offline / away
- MongoDB con Docker Compose
REST clients + Socket.io clients
↓
Express + Socket.io
↓
Controllers / event handlers
↓
Mongoose (User, Room, Message)
↓
MongoDB
git clone https://github.com/ENER90/chat-api.git
cd chat-api
npm install
cp .env.example .env
docker-compose up -d mongodb
npm run devServer: http://localhost:3004
### Messages
POST /api/messages/:roomId (auth) GET /api/messages/:roomId (auth) PUT /api/messages/:messageId (auth) DELETE /api/messages/:messageId (auth)
## WebSocket events
**Client → Server:** `join_room`, `leave_room`, `send_message`, `edit_message`, `delete_message`
**Server → Client:** `new_message`, `message_edited`, `message_deleted`, `user_joined`, `user_left`, `error`
## Example
```bash
curl -X POST http://localhost:3004/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"john","email":"john@test.com","password":"123456"}'
curl -X POST http://localhost:3004/api/rooms \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"name":"General Chat","description":"Main chat room","type":"public"}'
curl -X POST http://localhost:3004/api/messages/ROOM_ID \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"content":"Hello everyone!"}'
PORT=3004
MONGODB_URI=mongodb://admin:password123@localhost:27019/chat-db?authSource=admin
JWT_SECRET=your-secret-key
JWT_EXPIRES_IN=7d
CORS_ORIGIN=http://localhost:3004René Del Valle Rodríguez LinkedIn
MIT POST /api/auth/register POST /api/auth/login GET /api/auth/me (auth)
### Rooms
POST /api/rooms (auth) GET /api/rooms GET /api/rooms/:id (auth) POST /api/rooms/:id/join (auth) POST /api/rooms/:id/leave (auth)