A chat server built with Go and WebSockets. I made this to learn Go's concurrency patterns.
Live link: https://websocket-chat-production-8e24.up.railway.app
- Real-time messaging between users
- Shows typing indicators
- Username validation and join/leave notifications
- Caps at 50 users so the server doesn't crash
- Simple web interface included
- Messages disappear when you refresh (no database)
Clone and run:
git clone https://github.com/dax-side/websocket-chat.git
cd websocket-chat
go mod download
go run main.goOr build first:
go build -o chat-server main.go
./chat-serverOpen http://localhost:8080 in your browser. Try multiple tabs with different usernames.
The server creates a Hub that manages all connected users. When someone joins, the Hub spawns two goroutines for them: one reads their messages, one sends messages to them.
Browser ←→ Client ←→ Hub ←→ All Other Clients
Key parts:
- Hub: Coordinates everything, handles joins/leaves/broadcasts
- Client: Represents one connected user
- Goroutines: Each user gets two background workers
- Channels: How goroutines talk to each other
- Mutex: Prevents race conditions when updating the user list
- Goroutines for concurrent I/O
- Channels for passing messages between goroutines
- Select statements for multiplexing channels
- Mutex for protecting shared data
- WebSocket connection handling
- JSON encoding/decoding
- Go 1.22+
- gorilla/websocket package
- Plain HTML/CSS/JavaScript frontend
Change these in the code:
- Max users: 50 (in
NewHub()) - Port: 8080 (in
main()) - Username length: 20 chars max
MIT - do whatever you want with it.