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.
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 |
To run the full distributed network, you must execute four separate processes and then launch the web clients.
Install the required WebSocket library:
npm install ws
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 |
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 |
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.
-
Send messages from one client.
-
Verify all other clients receive them, regardless of the connected server/proxy.
-
Disconnect and reconnect a client.
-
It should receive the recent chat history stored in server.js.
This test confirms automated client-side recovery:
-
Identify which proxy your client is connected to (e.g., Proxy A on port 8080).
-
In Terminal 3, stop Proxy A (
Ctrl+C). -
The client will:
-
Detect the disconnection.
-
Wait 3 seconds.
-
Reconnect to Proxy B (8081).
-
-
Result: Chat resumes without user action.
MIT — Feel free to use, modify, and contribute!
Pull requests are welcome! Please open an issue first to discuss what you’d like to change.