Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 2.03 KB

README.md

File metadata and controls

42 lines (35 loc) · 2.03 KB

Two Node Chat

A real-time chat application demonstrating WebSocket connections distributed across multiple Node.js instances using RabbitMQ and Nginx.

Architecture

       Client 1         Client 2
          \                /
           \              /
            v            v
          [  Nginx Proxy  ] (Load Balancer)
            /            \
           v              v
      [ App Node 1 ]   [ App Node 2 ] (WebSocket Servers)
           \              /
            \            /
             v          v
          [   RabbitMQ   ] (Message Broker / Fanout Exchange)

How It Works

  • Nginx: Serves the static HTML/JS client and proxies /ws requests to the upstream Node.js servers, load-balancing connections (round-robin).
  • WebSockets: Maintains an open, real-time connection between the browser and a specific Node.js server instance.
  • RabbitMQ: The Node.js instances publish incoming chat messages to a RabbitMQ fanout exchange. Each instance has an exclusive queue bound to this exchange. When a message is received from the queue, the instance broadcasts it to all its locally connected WebSocket clients. This ensures messages sent to one node are distributed to clients on all other nodes.

Running the Application

  1. Make sure you have Docker and Docker Compose installed.
  2. Run the following command in this directory:
    docker compose up --build
  3. Open http://localhost:8090 in your browser.

Demo

  1. Open http://localhost:8090 in two different browser tabs.
  2. Enter a different username in each tab and connect.
  3. Check the chat window to see which Server ID ([node-1] or [node-2]) each client is connected to.
  4. Send messages between the tabs. You will see messages seamlessly delivered across nodes.
  5. Stop one of the application nodes (e.g., docker stop two-node-chat-app1-1).
  6. The client connected to the stopped node will automatically reconnect (after a 2-second delay) to the surviving node, and you can continue chatting without losing the ability to send messages.