Skip to content

0daysleft/Simple-Distributed-Chat-App

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💬 Distributed Chat Application (Web UI & Failover)

This project implements a complete, fault-tolerant distributed chat system using Node.js. It features a modern web interface and robust client-side failover logic, allowing users to stay connected even if a primary server node fails.


🏗️ Architecture Overview

The system is designed with scalability and high availability in mind, relying on three specialized components that communicate over two different network protocols.

Component Role Protocol Technology
server.js Backend Core. Manages raw TCP connections, handles message persistence (history), and relays messages across peer servers to synchronize the chat. TCP Sockets Node.js net
websocket_proxy.js Protocol Bridge. Translates WebSocket traffic (from the browser) into raw TCP packets (for the server network) and performs the reverse translation. WS ↔ TCP Node.js ws, net
index.html Frontend UI. Provides the user interface and implements the critical client-side failover logic to maintain connection if a component fails. WebSockets HTML, Tailwind CSS, JS

🚀 Setup and Running Instructions

To run the full distributed network, you must execute four separate processes and then launch the web clients.


🧩 Step 1: Install Dependencies

Install the required WebSocket library:

npm install ws

🖥️ Step 2: Start Server Nodes (Backend Core)

Open two terminal windows. These servers establish the peer-to-peer network.

Terminal Role Command Notes
Terminal 1 Server A (TCP 3000) node server.js 3000 3001 Peers with 3001
Terminal 2 Server B (TCP 3001) node server.js 3001 3000 Peers with 3000

🌉 Step 3: Start WebSocket Proxies (The Bridge)

Open two more terminal windows. These proxies link the browser clients to the TCP backend.

Terminal Role Command Linkage
Terminal 3 Proxy A (WS 8080) node websocket_proxy.js 8080 3000 WS 8080 ↔ TCP 3000
Terminal 4 Proxy B (WS 8081) node websocket_proxy.js 8081 3001 WS 8081 ↔ TCP 3001

🌐 Step 4: Launch Web Clients

Open two browser tabs or windows:

Navigate directly to the local index.html file.

Enter a unique username in each tab.

Click "Join Chat" to initiate the connection.

🧪 Testing Functionality

1. ✅ Distributed Broadcast

  • Send messages from one client.

  • Verify all other clients receive them, regardless of the connected server/proxy.

2. 💾 Message Persistence

  • Disconnect and reconnect a client.

  • It should receive the recent chat history stored in server.js.

3. 🔁 Client Failover (Critical Fault Tolerance Test)

This test confirms automated client-side recovery:

  1. Identify which proxy your client is connected to (e.g., Proxy A on port 8080).

  2. In Terminal 3, stop Proxy A (Ctrl+C).

  3. The client will:

    • Detect the disconnection.

    • Wait 3 seconds.

    • Reconnect to Proxy B (8081).

  4. Result: Chat resumes without user action.

📄 License

MIT — Feel free to use, modify, and contribute!

🙌 Contributing

Pull requests are welcome! Please open an issue first to discuss what you’d like to change.

About

A fault-tolerant, distributed chat application with a modern web UI, built in Node.js. Features client-side failover, message persistence, and real-time communication across peer-to-peer server nodes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors