A high-performance, real-time chat application built with Go and WebSockets. This chat application supports multiple rooms, user presence tracking, and provides a modern web interface.
- 🔌 Real-Time Messaging: Instant message delivery using WebSocket connections
- 🏠 Multiple Rooms: Create and join different chat rooms
- 👥 User Presence: See who joins and leaves in real-time
- 📊 Room Statistics: Track active users per room
- 🔐 Authentication Middleware: Simple username-based authentication
- ⚡ Graceful Shutdown: Proper cleanup of resources on server shutdown
- 🎨 Modern UI: Clean and responsive web interface
- 🚀 High Performance: Concurrent client handling with goroutines
The application follows a clean architecture pattern with separation of concerns:
chat-app/
├── cmd/
│ └── main.go # Application entry point
├── internal/
│ ├── auth/
│ │ └── middleware.go # Authentication middleware
│ ├── client/
│ │ └── client.go # WebSocket client management
│ ├── hub/
│ │ └── hub.go # Central hub for message broadcasting
│ └── message/
│ └── message.go # Message data structures
├── pkg/
│ └── websocket/
│ └── websocket.go # WebSocket handler
└── web/
├── index.html # Frontend HTML
├── style.css # Styling
└── app.js # Frontend JavaScript
The central message broker that manages:
- Client registration/unregistration
- Room-based message broadcasting
- Client lifecycle management
- Graceful shutdown handling
Individual WebSocket connection handling:
- Message reading and writing
- Heartbeat/ping-pong mechanism
- Connection cleanup
HTTP upgrade to WebSocket and connection management
Simple authentication layer to validate user sessions
- Go 1.25.3 or higher
- A modern web browser
Run the server with:
go run ./cmd/main.goThe server will start on http://localhost:8080
- Open your browser and navigate to
http://localhost:8080 - Enter a username
- Choose or create a room (default: "general")
- Click "Join Chat"
- Start chatting!
GET /- Serves the web interfaceWebSocket /ws?username=<name>&room=<room>- WebSocket connectionGET /health- Health check endpointGET /stats- Room statistics (returns JSON with active users per room)
You can customize the server by modifying cmd/main.go:
:8080- Server port (line 58)- CORS origins - Currently allows all origins (line 50)
- Read/Write/Idle timeouts (lines 60-62)
The application supports several message types:
chat- Regular chat messagesjoin- User joined notificationleave- User left notificationerror- Error messagestyping- Typing indicators
- Implement proper JWT-based authentication
- Add rate limiting
- Validate and sanitize all inputs
- Use secure WebSocket (WSS) in production
- Implement proper CORS policies
- Add message encryption for sensitive data
- Implement proper logging and monitoring
- Go - Backend programming language
- Gorilla WebSocket - WebSocket implementation
- rs/cors - CORS middleware
- Vanilla JavaScript - Frontend (no frameworks)
- HTML5 & CSS3 - Modern web interface
- Handles multiple concurrent connections efficiently
- Uses buffered channels to prevent blocking
- Implements proper connection pooling
- Graceful degradation on connection errors
The project follows Go best practices with:
- Clean separation between handlers, models, and business logic
- Package-level organization
- Context-based cancellation for graceful shutdowns
- Safe concurrent access with mutexes
To extend the application:
- Add new message types in
internal/message/message.go - Implement business logic in
internal/hub/hub.go - Add new endpoints in
cmd/main.go - Update frontend in
web/directory
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.

Happy Chatting! 💬