SocketChat-Go is a lightweight, concurrent TCP chat server built purely in Go using the standard library. It enables multiple users to connect, log in with a username, and chat with each other in real time over TCP. The project demonstrates clean concurrency patterns, robust user management, and socket-based communication.
- Overview
- Architecture
- Features
- Tech Stack
- Folder Structure
- Connection Flow
- Message Commands
- Timeout & Heartbeat
- Running Locally
- Example Interaction
- Logging
- Demo Video
SocketChat-Go provides a real-time TCP chat environment where clients connect directly to the server via sockets. Each user logs in with a unique username and can send messages, direct messages, or check active users.
The system supports at least 5–10 concurrent clients, handles disconnects gracefully, and includes idle timeouts and heartbeat responses.
The system follows a simple but scalable architecture:
- main.go — entry point that sets up TCP listener and accepts clients
- Hub struct — manages all connected users and broadcasts messages
- Client struct — encapsulates each connection and outbound channel
- handleConn — handles login, message parsing, and dispatch
- clientWriter — asynchronous writer for each client
- Utility functions — normalize inputs, handle errors safely, and manage timers
Key design principles:
- Non-blocking broadcast using buffered channels
- Mutex-protected global state (
Hub) - Graceful cleanup on disconnect
- Safe idle timeout handling with proper timer reset pattern
- Structured logs for easy debugging
Completed:
- Concurrent connection handling with goroutines
- LOGIN command for unique usernames
- MSG command for public messages
- DM command for private messages
- WHO command to list all active users
- PING → PONG heartbeat response
- Idle timeout (auto-disconnect after 60s inactivity)
- Graceful shutdown via Ctrl+C
- Structured logging and error-safe writes
Pending (Future Enhancements):
- Room-based chat support
- Encrypted connections (TLS)
- Authentication via tokens or password system
- Message persistence (to files or DB)
- WebSocket adapter for browser clients
- Language: Go 1.22+
- Networking: net + bufio (standard library only)
- Concurrency: Goroutines + Channels
- Logging: log package
- CLI Support: Flags & environment variables
.
├── main.go
├── README.txt
- Server listens on port 4000 (or from ENV/flag).
- Each client connects via TCP (e.g., using ncat or telnet).
- On connection, user must log in using:
LOGIN <username> - If username is unique → server replies
OK, elseERR username-taken. - Once logged in, users can send messages or commands.
- On disconnect, all users are notified with:
INFO <username> disconnected.
| Command | Description | Example |
|---|---|---|
| LOGIN | Log in with unique name | LOGIN Naman |
| MSG | Send public message | MSG Hello everyone! |
| DM | Send private message | DM Yudi hey there! |
| WHO | List active users | WHO |
| PING | Heartbeat | PING → PONG |
- Idle Timeout: Users are disconnected after 60s of inactivity.
The server sends:
INFO disconnected due to inactivity - Heartbeat:
Clients can periodically sendPING, and the server respondsPONG.
- Go 1.22+ installed
- ncat or telnet for client connections
go run .
By default, the server runs on localhost:4000.
In two separate terminals:
ncat localhost 4000
LOGIN Naman
MSG hi everyone!
ncat localhost 4000
LOGIN Yudi
DM Naman hey this is private!
Client 1 (Naman):
LOGIN Naman
OK
MSG hi everyone!
MSG Yudi hello Naman!
DM Yudi hey this is private!
Client 2 (Yudi):
LOGIN Yudi
OK
MSG hello Naman!
Server Output:
[CONNECT] new connection from 127.0.0.1:55342
[LOGIN] user=Naman
[MSG] from=Naman text="hi everyone!"
[DM] from=Yudi to=Naman text="hey this is private!"
[DISCONNECT] Naman disconnected
All major actions are logged in the server terminal with structured context:
[CONNECT],[LOGIN],[MSG],[DM],[DISCONNECT],[ERROR]
This helps debug or audit chat interactions easily.
Demo Recording:
Watch the full demonstration of SocketChat-Go in action here:
https://www.loom.com/share/658aed602c1448a4b8e3ec29be90c29a