Skip to content

omr-muhammad/tcp-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TCP Chat Application

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.

Features

  • 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

Prerequisites

  • Node.js (v14 or higher recommended)
  • npm or yarn

Installation

  1. Clone the repository
  2. Install dependencies:
npm install
  1. Create a .env file in the root directory:
NODE_ENV=development
PORT=your.port.number
DEV_IP_ADDRESS=0.0.0.0
PROD_IP_ADDRESS=your.production.ip

Usage

Starting the Server

node --env-file=.env --watch server.js

The server will start listening on the configured host and port (default: 0.0.0.0:${PORT}).

Connecting as a Client

Open a new terminal window and run:

node client.js

You can open multiple terminals to simulate multiple users chatting.

Chat Commands

  • Type your message and press Enter to send
  • Type .exit to disconnect gracefully
  • Press Ctrl+C to force quit

Project Structure

├── 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

Technical Details

Message Protocol

Messages are delimited using a custom delimiter {[$#%#$]} to handle TCP streaming and chunk reassembly.

Spam Protection

  • 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

Connection Management

  • 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)

Security Considerations

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

License

MIT

Contributing

Feel free to submit issues or pull requests for improvements.

About

In this project I used the node net module to create a tcp server and handle the client/server connection logic

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors