This Proof of Concept (POC) project demonstrates the scaling capabilities of a WebSocket server implemented in Golang, utilizing the Gorilla WebSocket library. The project includes containerization with Docker and deployment on Kubernetes with features like load balancing, preventing direct client communication, and integrating Redis Pub/Sub for efficient message broadcasting.
- Golang installed on your machine
- Docker installed
- Kubernetes cluster set up (Minikube for local development)
- kubectl configured to communicate with your cluster
- Redis server running (locally or in a cluster)
-
Install Minikube:
brew install minikube
-
Start Minikube:
minikube start
-
Clone the repository:
git clone https://github.com/manavendrasen/scalable-chat-app.git cd scalable-chat-app
-
Build the WebSocket server Docker image:
docker build -t <your-user-name>/scalable-chat-app .
-
Push the docker image to a registry and modify the
k8.yaml
file to use your docker image (line 55)docker push <your-user-name>/scalable-chat-app
-
Deploy the WebSocket server and redis on Minikube:
kubectl apply -f k8.yaml
-
Verify the deployment:
kubectl get pods
main.go
: Contains the Golang WebSocket server code.Dockerfile
: Dockerfile for containerization.k8.yaml
: Kubernetes deployment files.README.md
: Documentation.
The WebSocket server is implemented using Golang and the Gorilla WebSocket library. Goroutines are used to handle concurrent connections efficiently.
The WebSocket server is containerized using Docker, providing portability and ease of deployment.
The WebSocket server is deployed on Minikube with three replicas and a LoadBalancer service, showcasing horizontal scaling and load balancing. Clients are isolated from direct communication.
Redis Pub/Sub is integrated to enable broadcasting messages on a specific channel. This feature enhances real-time communication across WebSocket server instances. Without redis all the pods were independent and Clients connected to different pods could not connect to each other.
Redis acts as a store for chats.
Redis is added to the Minikube deployment to support the Pub/Sub functionality. The deployment configuration can be found in k8.yaml
.
- Connect to the WebSocket server using your preferred WebSocket client, specifying the LoadBalancer service IP.
For local testing - Get Minikube service url
minikube service chat-app-service
Copy the url and open postman to connect to the websocket ws://host:port/ws
To remove the deployed resources:
kubectl delete -f k8.yaml
minikube stop
minikube delete
This project demonstrates the scalability and real-time communication capabilities of a WebSocket server in a containerized and Minikube Kubernetes environment, with the added benefit of Redis Pub/Sub for efficient message broadcasting. Feel free to explore and extend the project for your specific use cases. This is just a POC to test if this was possible, do not use this in prod :).