This repository contains the Chat Service app, a high-performance real-time messaging platform built using FastAPI, Socket.io, and MongoDB. The Chat Service app provides a robust and feature-rich environment for users to engage in both group chats and private chats with seamless real-time communication.
- Real-Time Messaging: Powered by FastAPI and Socket.io for fast and real-time communication.
- Group and Private Chats: Create and join group or private chat rooms with ease.
- User Authentication: Uses JWT-based authentication for secure access to chat rooms.
- Admin Panel: Manage users and chat rooms with admin APIs.
- Online Users: View the list of online users in chat rooms.
- Multimedia Support: Send text messages, images, and files in chat rooms.
Chat-Service\server
: Contains all backend code, including FastAPI, database connections, and API routes.Chat-Service\client
: Contains the frontend code for the chat application built with Node.js.
You can download and install MongoDB and Mongosh from the official MongoDB website. Follow the instructions for your operating system to install both.
After installing MongoDB, perform the following steps:
-
Start MongoDB:
- On Windows:
- Open your terminal.
- Navigate to the MongoDB installation directory:
cd "C:\Program Files\MongoDB\Server\<#version>\bin"
- Run:
mongod
- On Linux or macOS: Run:
sudo systemctl start mongod
- On Windows:
-
Create the
chat_service
database:- Open Mongosh and run:
use chat_service
- Open Mongosh and run:
-
Create a username and password for the
chat_service
database:db.createUser({ user: "your_username", pwd: "your_password", roles: [{ role: "readWrite", db: "chat_service" }] })
-
Restart MongoDB to apply the changes:
- On Windows:
- Press
Ctrl+C
inside MongoDB installation directory to stop its server. - Run
mongod
.
- Press
- On Linux or macOS: Run:
sudo systemctl restart mongod
- On Windows:
Make sure you have Python installed, then:
- Navigate to the project's root directory.
- Install the dependencies from
requirements.txt
:pip install -r requirements.txt
You need to create two .env
files
- one for the backend (
Chat-Service\server\.env
) and one for the frontend (Chat-Service\client\.env
).
Backend (Chat-Service\server\.env
):
SECRET_KEY=<your_generated_secret_key>
MONGO_USER=<your_mongo_username>
MONGO_PASSWORD=<your_mongo_password>
MONGO_HOST=localhost
MONGO_PORT=27017
MONGO_DB=chat_service
Frontend (Chat-Service\client\.env
):
REACT_APP_API_URL=http://localhost:8000
Generating a Secret Key You can generate a secret key using one of the following methods:
- Python:
import secrets
print(secrets.token_urlsafe(32))
- OpenSSL:
openssl rand -base64 32
- Django Secret Key Generator (online): link
Navigate to the backend directory (Chat-Service\server
) and start the FastAPI server:
cd Chat-Service\server
uvicorn main:chat_service_app --reload
You can access the FastAPI API documentation at http://localhost:8000/docs.
To test the app's Socket.io capabilities, you'll need to install Node.js from the official Node.js website. Then, navigate to the frontend directory (Chat-Service\client
) and run the following commands:
cd Chat-Service\client
npm install
npm start
You can test the frontend at http://localhost:3000.
Ensure that you have Docker and Docker Compose installed. You can download Docker from Docker's official website.
git clone https://github.com/AmirhsFar/danesh-sazan-internship-tasks.git
cd danesh-sazan-internship-tasks
Before running the application, you need to securely create the MongoDB username, password, and the FastAPI secret key using Docker Secrets. Here’s how you can do that:
- Create Docker Secrets for MongoDB Username:
echo "your_mongo_user" | docker secret create mongo_user -
- Create Docker Secrets for MongoDB Password:
echo "your_mongo_password" | docker secret create mongo_password -
- Create Docker Secret for FastAPI Secret Key:
- Generate a secure secret key using Python (or any method you prefer):
python3 -c "import secrets; print(secrets.token_hex(32))"
- Then, create the Docker Secret:
echo "your_generated_secret_key" | docker secret create secret_key -
Once the secrets are set up, build and start the services using Docker Compose with specifying environment variables such as MONGO_INITDB_DATABASE
, MONGO_DB
, MONGO_PORT
, MONGO_HOST
, and REACT_APP_API_URL
through the command:
MONGO_INITDB_DATABASE=chat_service \
MONGO_DB=chat_service \
MONGO_PORT=27017 \
MONGO_HOST=mongo \
REACT_APP_API_URL=http://localhost:8000 \
docker-compose up --build
MONGO_INITDB_DATABASE
is used by MongoDB to create thechat_service
database on initialization.MONGO_DB
,MONGO_PORT
, andMONGO_HOST
are used by the FastAPI service to connect to MongoDB.REACT_APP_API_URL
is used by the Node.js frontend to connect to the FastAPI backend.
The build command will build and run the following services:
- MongoDB (database)
- FastAPI (backend)
- Node.js (frontend)
- Nginx (reverse proxy for Node.js and FastAPI)
The application will be available at http://localhost/
.
Ensure that the MongoDB service is up and running by checking the logs:
docker logs mongo_db
You can also connect to the MongoDB instance using a MongoDB client or using mongosh to ensure the chat_service database has been created and that everything is set up properly.
The API documentation for FastAPI will be available at: http://localhost/api/docs.
The frontend will be accessible at: http://localhost/.
For a visual understanding of the Chat Service app's workflow, please refer to the Chat Service Flowchart.jpg
image inside the server
directory of the Chat-Service
folder.
- FastAPI: Backend framework for building APIs.
- Socket.io: Real-time communication between clients and servers.
- MongoDB: NoSQL database for storing chat data.
- Docker: Containerization for deploying the app.
- Nginx: Reverse proxy for routing requests to Node.js and FastAPI.
For more detailed API information, refer to the FastAPI Swagger docs at /docs
or check the code under the server
folder.