Skip to content

raulingg/simple-chat-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“£ Simple Chat Server

A lightweight, zero-dependency TCP chat server built with Node.js.
Supports multiple simultaneous connections, message broadcasting, and a simple terminal-based client.

badge-node


Table of Contents


✨ Features

  • πŸ”Œ 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

πŸ“ Project Structure

simple-chat-server/
β”‚
β”œβ”€β”€ simple-server.js # TCP chat server
└── simple-sender.js # Simple terminal client

πŸ› οΈ Requirements

  • Node.js 20+
  • Terminal for client connections
  • TCP port open (default is 4000 unless modified)

πŸ“¦ Installation

  1. 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.


πŸš€ Usage

πŸ–₯️ Running the Server

node simple-server.js

To 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.

πŸ’¬ Connecting Clients

Open a new terminal window/tab for each client, then run:

node simple-sender.js

Once connected, you can type messages in your terminal. They'll be sent to the server, which will broadcast them to all other connected clients.


βš™οΈ How It Works

πŸ–₯️ Server (simple-server.js)

  • 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

πŸ’» Client (simple-sender.js)

  • 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

🧩 Architecture Diagram

        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚      Chat Server        β”‚
        β”‚     (simple-server)     β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚ broadcasts
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚           β”‚              β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Client 1 β”‚ β”‚ Client 2  β”‚ β”‚ Client 3  β”‚
β”‚ sender.js β”‚ β”‚ sender.js β”‚ β”‚ sender.js β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


πŸ—ΊοΈ Roadmap

Potential improvements:

  • πŸ” Username / nickname system
  • πŸ”’ TLS encryption
  • πŸ“œ Message persistence
  • πŸ—‚οΈ Chat rooms / channels
  • 🌐 Web client using WebSockets
  • πŸ” Auto-reconnect logic
  • 🐳 Dockerfile + Compose setup

About

Create a chat server using node:net module and stdout/stdin + node:readline as UI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published