A simple real-time chat application built with Node.js TCP sockets. Users can connect to a central server and exchange messages with all connected clients.
- Real-time messaging between multiple clients
- User join/leave notifications
- Anti-spam protection with automatic kick
- Connection timeout handling (30 seconds)
- Clean terminal UI with message alignment
- Graceful shutdown handling
- Node.js (v14 or higher recommended)
- npm or yarn
- Clone the repository
- Install dependencies:
npm install- Create a
.envfile in the root directory:
NODE_ENV=development
PORT=your.port.number
DEV_IP_ADDRESS=0.0.0.0
PROD_IP_ADDRESS=your.production.ipnode --env-file=.env --watch server.jsThe server will start listening on the configured host and port (default: 0.0.0.0:${PORT}).
Open a new terminal window and run:
node client.jsYou can open multiple terminals to simulate multiple users chatting.
- Type your message and press Enter to send
- Type
.exitto disconnect gracefully - Press
Ctrl+Cto force quit
├── server.js # TCP server handling multiple client connections
├── client.js # TCP client with interactive chat interface
├── globals.js # Shared constants (delimiter, message limit)
├── spammer.js # Test script for spam detection
├── .env # Environment configuration
└── .gitignore # Git ignore rules
Messages are delimited using a custom delimiter {[$#%#$]} to handle TCP streaming and chunk reassembly.
- Maximum message buffer size: 1024 bytes (1 KB)
- Clients exceeding this limit without sending the delimiter are automatically kicked
- Test spam behavior using
node spammer.js
- Each client receives a unique ID upon connection
- Server broadcasts join/leave/disconnect notifications to all clients
- Automatic cleanup on timeout (30 seconds of inactivity)
This is a basic implementation intended for educational purposes. For production use, consider adding:
- Authentication and authorization
- Message encryption (TLS/SSL)
- Rate limiting per client
- Input sanitization
- Message size validation
- IPv6 support
MIT
Feel free to submit issues or pull requests for improvements.