Real-time Chat Server with Rooms
Build a chat server in Go where people join rooms and chat in real time over WebSockets, using the gorilla/websocket library. Each room has its own members and message history, so someone who joins sees the last messages. The server is designed so that many connections run safely at the same time: each client has one goroutine reading and one writing, and a central hub coordinates rooms through channels instead of shared memory and locks. Slow clients can't block everyone else, and dead connections are detected with ping and pong. At the end, you'll load test it with thousands of simulated clients on your own machine. You'll learn WebSockets, goroutines and channels, the hub pattern, backpressure, and testing concurrent servers.
- Estimate
- ~14h
- Steps
- 7
- Completed by
- 0
- Proposed by
- codeseed.app
gorilla/websocket · goroutines · channels
Project roadmap
- 01
Accept WebSocket connections
~2hCreate a Go module with gorilla/websocket. Add an HTTP handler at /ws that upgrades the connection, checking the Origin header against an allowed list. Create a Client struct holding the connection, a name and a buffered send channel. Serve a minimal HTML page with JavaScript to connect and send messages. Done when: the browser page connects, and the server logs each connection.
- 02
Give each client a reader and a writer
~2hStart two goroutines per client: readPump reads messages (with a size limit set by SetReadLimit) and passes them to the hub, and writePump is the only goroutine writing to the connection, sending messages from the send channel. Add ping and pong with read deadlines, so dead connections are closed within a minute. Done when: messages flow in both directions, and an unplugged client is removed within a minute.
- 03
Build the hub
~2.5hCreate a Hub that runs in its own goroutine and owns all rooms, handling register, unregister and broadcast requests through channels, so no locks are needed. Messages are JSON with a type (join, leave, message), room and text. Broadcast a message to everyone in its room. When a client's send channel is full, drop that slow client rather than blocking the hub. Done when: several browsers in the same room see each other's messages.
- 04
Join and leave rooms
~1.5hHandle join and leave messages: a client can be in several rooms, rooms are created on the first join and removed when the last member leaves, and joins and leaves are announced to the room. Validate room names and message length. Add a message listing the members of a room. Done when: clients can move between rooms and only receive messages from rooms they're in.
- 05
Keep recent history
~1.5hStore the last 50 messages of each room in a ring buffer owned by the hub. When a client joins a room, send them its history first. Add a timestamp and the sender's name to every message on the server, so clients can't fake them. Done when: someone joining a busy room sees the last 50 messages in order.
- 06
Load test
~2.5hWrite a Go load test program (only against your own local server) that opens thousands of WebSocket clients across 100 rooms, each sending a message every few seconds, and measures the time from sending to receiving. Record the number of connections, messages per second, latency percentiles and memory use in README.md. Done when: the server handles the load without errors, and the numbers are in README.md.
- 07
Write tests
~2hWrite tests using httptest.NewServer and real WebSocket clients: messages reach only their room, history is sent on join, slow clients are dropped without blocking others, and disconnects remove clients from rooms. Run them with -race. Done when: go test -race ./... passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack