This project demonstrates a real-time messaging system using Redis's Publish/Subscribe (Pub/Sub) feature, WebSocket for client communication, and MySQL for persistent message storage.
- Real-time Messaging: Users can publish and subscribe to channels to receive messages in real-time.
- WebSocket Communication: Uses WebSockets for efficient, bidirectional communication between the server and clients.
- Redis Pub/Sub: Leverages Redis's Pub/Sub mechanism for message broadcasting.
- MySQL Persistence: Stores all messages in a MySQL database for historical access.
- Graceful Shutdown: The server can be shut down gracefully with
Ctrl+C
orSIGTERM
. - Multiple Channel Subscriptions: Clients can subscribe to multiple channels simultaneously.
- Go: The programming language for the backend server.
- Redis: In-memory data structure store used for Pub/Sub.
- MySQL: Relational database for persistent message storage.
- Gorilla WebSocket: Go library for WebSocket implementation.
- go-redis: Go client for interacting with Redis.
- GORM: ORM library for interacting with MySQL.
- Prerequisites:
- Go (latest stable version recommended)
- Docker (for running Redis) (Recommended)
- Redis server (running on
localhost:6379
by default) - MySQL server (running on
localhost:3306
with achatdb
database) - Postman Or Any api client for testing Api
2.Docker For Redis and Mysql:
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack (Redis Setup)
docker run -d --name mysql_chat \
-e MYSQL_ROOT_PASSWORD=rootpassword \
-e MYSQL_DATABASE=chatdb \
-e MYSQL_USER=chatuser \
-e MYSQL_PASSWORD=chatpassword \
-p 3306:3306 \
--restart always \
mysql:latest
-
Clone the Repository:
git clone https://github.com/Amul-Thantharate/RediSocketDB.git cd RediSocketDB
-
Database Setup:
- Create a database named
chatdb
in your MySQL server. - The application will automatically create the
messages
table when it starts. - Make sure you have a user
nora
with passwordroot
with access to thechatdb
database. You can change this in themain.go
file.
- Create a database named
-
Install Dependencies:
go mod tidy
-
Run the Application:
go run main.go
You should see the message
Starting server on :8080
in your terminal.
- Endpoint:
POST /publish
- Parameters:
channel
: The channel to publish the message to.message
: The message content.
- Example (using
curl
):This will publish "Hello, world!" to the "general" channel and store it in MySQL.curl -X POST -d "channel=general&message=Hello, world!" http://localhost:8080/publish
- Endpoint:
/subscribe
- Query Parameters:
channel
: One or more channels to subscribe to (e.g.,/subscribe?channel=general&channel=news
).
- Example (using a WebSocket client):
- Connect to
ws://localhost:8080/subscribe?channel=general&channel=news
(to subscribe to both "general" and "news" channels). - You will receive messages from both channels in real-time.
- Connect to
- Example (using a browser): You can use a browser extension like "Simple WebSocket Client" to connect to the websocket.
- Press
Ctrl+C
in the terminal where the server is running. - The server will shut down gracefully, closing the Redis and MySQL connections.
- Database Connection Errors: Ensure your MySQL server is running, the database
chatdb
exists, and the usernora
has the correct permissions. Check the MySQL connection string inmain.go
. - Redis Connection Errors: Make sure your Redis server is running on the specified address and port (
localhost:6379
).
We welcome contributions! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature
). - Make your changes and commit them (
git commit -m "Your commit message"
). - Push your branch to your fork (
git push origin feature/your-feature
). - Open a pull request.
This project is licensed under the Apache License 2.0.