A lightweight, zero-dependency TCP chat server built with Node.js.
Supports multiple simultaneous connections, message broadcasting, and a simple terminal-based client.
- Features
- Project Structure
- Requirements
- Installation
- Usage
- How It Works
- Architecture Diagram
- Roadmap
- π Pure TCP server β no external dependencies
- π₯ Multiple-client support
- π’ Message broadcasting
- π₯οΈ Simple CLI interface
- πͺΆ Lightweight, ~50 lines of core logic
- π‘ Perfect starter project for learning sockets or building custom chat protocols
simple-chat-server/
β
βββ simple-server.js # TCP chat server
βββ simple-sender.js # Simple terminal client
- Node.js 20+
- Terminal for client connections
- TCP port open (default is 4000 unless modified)
-
Clone the repository:
git clone https://github.com/raulingg/simple-chat-server.git cd simple-chat-server
No dependencies to install β uses only built-in Node modules.
node simple-server.jsTo change the port, edit the PORT constant in simple-server.js.
Tip: If you plan to run the server on a remote machine, make sure the port is open on the firewall.
Open a new terminal window/tab for each client, then run:
node simple-sender.jsOnce connected, you can type messages in your terminal. They'll be sent to the server, which will broadcast them to all other connected clients.
- Creates a TCP server using
net.createServer() - Maintains an internal list of active client sockets
- When one client sends a message β broadcasts it to all other connected clients
- Cleans up the client list when a connection closes or errors occur
- Connects to the server using
net.Socket() - Captures user input from the terminal using
readline - Sends entered messages to the server over a TCP connection
- Displays broadcast messages received from the server in real time
βββββββββββββββββββββββββββ
β Chat Server β
β (simple-server) β
ββββββββββββ¬βββββββββββββββ
β broadcasts
βββββββββββββΌβββββββββββββββ
β β β
βββββββββββββ βββββββββββββ βββββββββββββ
β Client 1 β β Client 2 β β Client 3 β
β sender.js β β sender.js β β sender.js β
βββββββββββββ βββββββββββββ βββββββββββββ
Potential improvements:
- π Username / nickname system
- π TLS encryption
- π Message persistence
- ποΈ Chat rooms / channels
- π Web client using WebSockets
- π Auto-reconnect logic
- π³ Dockerfile + Compose setup