A scalable chat API built with Rust, utilizing WebSockets for real-time communication and MongoDB for persistent message storage.
- Real-time chat: Leverages WebSockets for instant message delivery and updates.
- Persistent storage: Stores chat messages in a MongoDB database for future retrieval.
- Scalable architecture: Designed with asynchronous programming and modular structure to handle a high volume of concurrent connections.
- Error handling: Implements custom error types and centralized error handling for graceful error responses.
- Logging: Utilizes structured logging for debugging and monitoring.
- CI/CD: Integrates with GitHub Actions for automated build, test, and deployment.
- Rust: Make sure you have Rust and Cargo installed on your system. You can follow the instructions at https://www.rust-lang.org/tools/install
- MongoDB: You'll need a running MongoDB instance. You can download and install it from https://www.mongodb.com/try/download/community
-
Clone the repository:
git clone <repository-url> cd chat-app
-
Set up environment variables:
-
Create a
.envfile in the project's root directory. -
Add the following environment variables:
MONGODB_URI="mongodb://localhost:27017" # Replace with your MongoDB connection URI MONGODB_DATABASE="chat_app" # Replace with your desired database name MONGODB_COLLECTION="messages" # Replace with your desired collection name SERVER_URL="127.0.0.1" # Replace with your server's URL SERVER_PORT="8080" # Replace with your desired port number RUST_LOG="info" # Set the desired log level (info, debug, etc.)
-
-
Build and run:
make build # Build the project make run # Run the server
You can use Docker and Docker Compose to easily build and run the chat API in a containerized environment.
- Docker: Make sure you have Docker and Docker Compose installed on your system. You can follow the instructions at https://docs.docker.com/get-docker/
-
Build the Docker images:
make docker-build
-
Start the containers:
make docker-up
-
Access the application:
- The chat API should now be accessible at
http://localhost:8080. - You can connect to the MongoDB instance using
mongodb://localhost:27017
- The chat API should now be accessible at
-
Stop the containers:
make docker-down
-
View logs:
make docker-logs
-
Connect to the WebSocket server:
- Use a WebSocket client library or tool to connect to
ws://SERVER_URL:SERVER_PORT(replaceSERVER_URLandSERVER_PORTwith the values from your.envfile).
- Use a WebSocket client library or tool to connect to
-
Send and receive messages:
-
The server expects messages in the following JSON format:
{ "username": "your_username", "content": "your_message", "timestamp": 1694844257 } -
When a client sends a message, the server will broadcast the updated list of all messages to all connected clients.
-
Contributions are welcome! Please feel free to open issues or submit pull requests.
This project is licensed under the Apache-2.0 License.
Remember:
- Replace placeholders like
<repository-url>,your_username, andyour_messagewith actual values. - Customize the
README.mdfurther to include any additional information relevant to your project, such as specific features, configuration options, or deployment instructions.
Feel free to ask if you have any more questions or modifications!