An open API service indexing awesome lists of open source software.

https://github.com/valentinocarmonas/realtimechat

A Real Time Chat API project
https://github.com/valentinocarmonas/realtimechat

docker expressjs jwt mongodb mvc-pattern nodejs real-time-chat socket-io tdd-javascript

Last synced: 3 months ago
JSON representation

A Real Time Chat API project

Awesome Lists containing this project

README

          

# Real-Time Chat System

![Build Status](https://github.com/ValentinoCarmonaS/RealTimeChat/actions/workflows/ci.yml/badge.svg)
[![Codecov](https://codecov.io/gh/ValentinoCarmonaS/RealTimeChat/branch/main/graph/badge.svg)](https://codecov.io/gh/ValentinoCarmonaS/RealTimeChat)

A **real-time chat application** built with **Node.js**, **Express**,
**Socket.IO**, and **MongoDB**, enabling multiple users to send and receive
messages instantly in a shared chat room. It implements the **MVC** pattern,
featuring a minimal frontend, persistent message storage, and easy deployment.

## πŸš€ Features

- **Real-Time Messaging**: Send and receive messages instantly using WebSockets.
- **Persistence**: Store messages in MongoDB for chat history.
- **Notifications**: Alerts for user connections and disconnections.
- **Minimal Interface**: Simple frontend with HTML, CSS, and JavaScript.
- **Testing**: Comprehensive automated tests with Jest and Supertest (93.75%
branch coverage).
- **Docker**: Containerized setup for easy deployment.

## πŸ“‹ Prerequisites

- **Node.js** v16+
- **MongoDB Atlas** or a local MongoDB instance
- **Docker** and **Docker Compose** (optional, for containerized setup)
- Modern web browser (Chrome, Firefox, etc.)

## πŸ› οΈ Installation

1. Clone the repository:

```bash
git clone https://github.com/ValentinoCarmonaS/RealTimeChat.git
cd RealTimeChat
```

2. Install dependencies:

```bash
npm install
```

3. Create a `.env` file in the root directory with:

```bash
PORT=3000
MONGODB_URI=mongodb+srv://:@cluster0.mongodb.net/realtime-chat?retryWrites=true&w=majority
```

## ▢️ Running the Application

- **Locally**:

```bash
npm start
```

The application will be available at `http://localhost:3000`.

- **With Docker**:

```bash
make build
make up
```

Stop containers with:

```bash
make down
```

## πŸ§ͺ Testing

Run tests with Docker:

```bash
make test
```

Or run tests with npm:

```bash
npm test
```

- View detailed reports in `coverage/lcov-report/index.html`.

## πŸ“š Main Functionalities

- **User Connection**: Users enter a username and join a shared chat room.
- **Messages**: Messages are sent in real-time, stored in MongoDB, and displayed
with the sender’s username.
- **History**: Retrieve historical messages when a new user connects.
- **Notifications**: Alerts when a user connects or disconnects.

## πŸ“‘ API Endpoints

The API is deployed and accessible at https://realtimechat-59t7.onrender.com.
The following RESTful endpoints are available under the
`https://realtimechat-59t7.onrender.com/api` base path. All endpoints require a
valid JWT token via the `Authorization Bearer` header unless otherwise noted.

### πŸ” Authentication

| Method | Endpoint | Description | Auth |
| ------ | -------------------- | ------------------- | ---- |
| POST | `/api/auth/login` | Authenticate user | ❌ |
| POST | `/api/auth/register` | Register a new user | ❌ |

### πŸ‘€ Users

| Method | Endpoint | Description | Auth |
| ------ | ---------------- | ------------------- | ---- |
| GET | `/api/users` | Get all users | βœ… |
| GET | `/api/users/:id` | Get a user by ID | βœ… |
| POST | `/api/users` | Create a new user | βœ… |
| PUT | `/api/users/:id` | Update a user by ID | βœ… |
| DELETE | `/api/users/:id` | Delete a user by ID | βœ… |

### 🧩 Rooms

| Method | Endpoint | Description | Auth |
| ------ | --------------- | ------------------- | ---- |
| GET | `/api/room` | Get all rooms | βœ… |
| POST | `/api/room` | Create a new room | βœ… |
| DELETE | `/api/room/:id` | Delete a room by ID | βœ… |

### πŸ’¬ Messages

| Method | Endpoint | Description | Auth |
| ------ | ------------------------------------ | ------------------------------------------- | ---- |
| GET | `/api/message?roomId=&limit=<#>` | Get messages for a room (requires `roomId`) | βœ… |
| POST | `/api/message` | Create a new message | βœ… |

> **Note**: Use the `roomId` query parameter to fetch messages for a specific
> room. The `limit` parameter is optional to control the number of returned
> messages.

### Example Interaction

```
User enters their name: "Ana"
Ana sends: "Hello, how are you?"
All users see: "Ana: Hello, how are you?" (stored in MongoDB)
Ana disconnects, all users see: "Ana has disconnected"
```

For more details, refer to the [technical specification](docs/statement.md).

## πŸ“š API Documentation

The API is documented using **Swagger (OpenAPI)**. Access the interactive documentation at:
```bash
http://localhost:3000/api-docs
```
or in production at:
```bash
https://realtimechat-59t7.onrender.com/api-docs
```

Explore endpoints, test requests, and view response examples directly in the Swagger UI.

## πŸ—‚οΈ Project Structure

```
RealTimeChat/
β”œβ”€β”€ backend/
β”‚ β”œβ”€β”€ src/
β”‚ β”‚ β”œβ”€β”€ config/ # Environment and database setup
β”‚ β”‚ β”œβ”€β”€ controllers/ # Business logic for WebSockets and messages
β”‚ β”‚ β”œβ”€β”€ middlewares/ # Validation and error handling
β”‚ β”‚ β”œβ”€β”€ models/ # MongoDB schemas for messages
β”‚ β”‚ β”œβ”€β”€ routes/ # HTTP routes for frontend and auxiliary endpoints
β”‚ β”‚ β”œβ”€β”€ sockets/ # WebSocket-specific logic
β”‚ β”‚ β”œβ”€β”€ tests/ # Unit and integration tests
β”‚ β”‚ β”œβ”€β”€ app.js # Express application setup
β”‚ β”‚ └── server.js # Server init file
β”œβ”€β”€ .env # Environment variables
β”œβ”€β”€ Dockerfile # Docker configuration
β”œβ”€β”€ docker-compose.yml # Docker Compose setup
β”œβ”€β”€ Makefile # Automation scripts
└── package.json # Dependencies and scripts
```

## πŸ“œ License

[MIT](LICENSE)