Skip to content

logeshkannan96/distributed-websockets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Distributed WebSocket System

This project demonstrates a distributed WebSocket system with load balancing, multiple WebSocket servers, and a Redis-based message broker. The system is containerized using Docker and includes a client application built with Angular.

Architecture

The system consists of the following components:

  1. WebSocket Servers (2 instances)

    • Node.js-based WebSocket servers
    • Each server has a unique ID
    • Uses Redis for message broadcasting between servers
    • Handles WebSocket connections and message routing
  2. Load Balancer (Nginx)

    • Distributes WebSocket connections across multiple servers
    • Provides high availability and scalability
    • Configured for WebSocket protocol support
  3. Redis

    • Acts as a message broker between WebSocket servers
    • Enables real-time message broadcasting across all server instances
    • Persists data using a Docker volume
  4. Client Application (Angular)

    • Modern web interface for interacting with the WebSocket system
    • Real-time message display and interaction
    • Responsive design

Prerequisites

  • Docker and Docker Compose
  • Node.js (for local development)
  • Angular CLI (for local client development)

Getting Started

  1. Clone the repository

    git clone https://github.com/logeshkannan96/distributed-websockets.git
    cd distributed-websocket
  2. Start the system using Docker Compose

    docker-compose up --build

    This will start:

    • Two WebSocket server instances
    • Nginx load balancer
    • Redis server
    • The client application will be available at http://localhost:4200
  3. For local development

    Server Development:

    cd server
    npm install
    npm run dev

    Client Development:

    cd client
    npm install
    ng serve

Configuration

WebSocket Servers

  • Server instances are configured through environment variables in docker-compose.yaml
  • Each server has a unique SERVER_ID
  • Redis connection is configured via REDIS_URL

Load Balancer

  • Nginx configuration is located in lb/nginx.conf
  • Load balancing strategy can be modified in the configuration file

Redis

  • Redis runs on the default port 6379
  • Data persistence is configured through Docker volumes

Testing the System

  1. Open multiple browser windows to http://localhost:4200
  2. Connect to the WebSocket server
  3. Send messages from different clients
  4. Observe how messages are broadcasted across all connected clients
  5. Verify that the load balancer distributes connections across both server instances

Monitoring

  • Redis data can be monitored using Redis CLI:

    docker exec -it distributed-websocket_redis_1 redis-cli
  • Server logs can be viewed using:

    docker-compose logs -f websocket-server-1
    docker-compose logs -f websocket-server-2

Troubleshooting

  1. Connection Issues

    • Verify all containers are running: docker-compose ps
    • Check server logs for errors
    • Ensure Redis is accessible from WebSocket servers
  2. Load Balancer Issues

    • Check Nginx configuration
    • Verify WebSocket server health
    • Check Nginx logs: docker-compose logs nginx

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

API Usage

Sending Messages

You can send messages to the WebSocket system using the REST API endpoint. Here are some examples:

  1. Send a message using curl:

    curl --location 'http://localhost/api/send-message' \
    --header 'Content-Type: application/json' \
    --data '{
        "username": "kattappa",
        "message": "Swami, I have a confession to make"
    }'
  2. Send a message with different content:

    curl --location 'http://localhost/api/send-message' \
    --header 'Content-Type: application/json' \
    --data '{
        "username": "bahubali",
        "message": "Kattappa, yes tell me"
    }'

The API accepts POST requests with the following JSON structure:

{
    "username": "string",
    "message": "string"
}

Note: A similar implementation using Server-Sent Events (SSE) with Redis Pub/Sub is available in the following GitHub repository: 👉 logeshkannan96/sse-redispubsub Feel free to check it out for reference or inspiration.

About

This project demonstrates a distributed WebSocket system with load balancing, multiple WebSocket servers, and a Redis-based message broker. The system is containerized using Docker and includes a client application built with Angular.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors