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: 5 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
```

- Tests cover user connections, message sending and storage, and WebSocket event logic.
- Coverage: 93.75% branches, 98.64% statements.
- 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.

### 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/spec.md).

## πŸ—‚οΈ 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
β”œβ”€β”€ frontend/
β”‚ β”œβ”€β”€ index.html # Main interface
β”‚ β”œβ”€β”€ styles.css # Basic styles
β”‚ └── script.js # Frontend logic
β”œβ”€β”€ .env # Environment variables
β”œβ”€β”€ Dockerfile # Docker configuration
β”œβ”€β”€ docker-compose.yml # Docker Compose setup
β”œβ”€β”€ Makefile # Automation scripts
└── package.json # Dependencies and scripts
```

## πŸ“œ License

[MIT](LICENSE)