This project implements a WebSocket-based chat server in Go using the Gorilla WebSocket package. The server supports basic message broadcasting between clients and is designed to handle multiple clients simultaneously. Each client is identified by a unique userId
.
- WebSocket Communication: Real-time messaging using WebSocket connections.
- Client Management: Register and unregister clients dynamically.
- Message Broadcasting: Send messages to specific clients.
- Graceful Handling of Disconnections: Handles unexpected client disconnections and cleans up resources.
- Redis Support: Planned integration with Redis for handling client presence and message queuing, enabling a scalable and distributed system.
internal/Client.go
: Manages individual client connections, handling both incoming and outgoing messages.internal/Hub.go
: Manages all connected clients, handling registration, unregistration, and message broadcasting.
-
Clone the Repository
git clone https://github.com/tarun-kavipurapu/chat-app.git cd chat-app
-
Install Dependencies
This project uses Gorilla WebSocket. To install the package, run:
go get github.com/gorilla/websocket
-
Run the Server
go run main.go
Clients can connect to the server using the WebSocket protocol. The server listens for connections and handles communication based on the client-defined userId
.
Messages are sent and received in JSON format. Each message should include the following fields:
from
: The sender's userIdto
: The recipient's userIdcontent
: The message content
{
"from": "user1",
"to": "user2",
"content": "Hello, World!"
}
To enable Redis support:
-
Install Redis Client Library
go get github.com/go-redis/redis/v8
-
Configure Redis in the Project
Modify the project to use Redis for storing active clients and message queuing. This will allow for scalability and distribution of the WebSocket server across multiple instances.
- Implement Redis-based message queuing and presence tracking.
- Develop a more robust client reconnection strategy.
- Add support for room-based messaging.
- Implement logging and monitoring features.
Contributions are welcome! Please open an issue or submit a pull request with your changes.
=