A simple TCP chat app built with Node.js (no other runtime or packages required).
What's included in this project:
- User login (
LOGIN <username>) and broadcast messaging (MSG <text>). - Direct messages (
DM <username> <text>) and list active users (WHO). - Idle timeout: the server disconnects clients after 60 seconds of inactivity.
- Node.js (v22+ recommended)
server.js— server implementationclient.js— client implementation
Start the server in one terminal:
node server.jsStart a client (open another terminal per client):
node client.jsDefault port: 4000. You can set PORT environment variable for the server if you want a different port.
LOGIN <username>— log in with a usernameMSG <text>— broadcast message to all other usersDM <username> <text>— send private message tousernameWHO— list other active usersexitorEXIT— quit the client
Heartbeat/idle behaviour:
- If no activity from client for 60 seconds, server will send
INFO idle-timeoutand close the connection.
Client input:
LOGIN alice
Server response (to that client):
OK
Alice wants to DM Bob: Client (Alice) input:
DM bob Hey Bob — are you there?
If Bob is online, Bob receives:
DM FROM alice Hey Bob — are you there?
Client input:
WHO
Client receives one line per other user, e.g.:
USER bob
USER charlie
Terminal A (server):
$ node server.js
Server is running on PORT : 4000
> INFO alice connected
> INFO bob connected
> INFO alice disconnected (idle-timeout)
Terminal B (client: alice):
> LOGIN alice
OK
> MSG Hello everyone
> DM bob Hi Bob
> WHO
> USER bob
> USER charlie
Terminal C (client: bob):
> LOGIN bob
OK
MSG alice Hello everyone
DM FROM alice Hi Bob
-
Idle timeout and intervals are configurable in the code as constants:
IDLE_TIMEOUT_MS(server) — default60_000(60s)IDLE_CHECK_INTERVAL_MS(server) — default10_000(10s)