A small real-time chatroom demo built with Go, Gorilla WebSocket, and plain HTML/CSS/JavaScript. It includes local user registration, session-based login, message history, emoji/stickers, and optional local Ollama AI assistant commands.
- Register, login, and logout
- Protected chatroom page and protected WebSocket endpoint
- Real-time multi-user chat over WebSocket
- Online user count
- System join/leave notifications
- Last 20 message history replay on reconnect
- Text, emoji, and local SVG sticker messages
- Distinct styles for self, other users, system messages, stickers, history, and AI messages
- Message length limit and character counter
- WebSocket connection status indicator (connected / disconnected / reconnecting)
- Auto reconnect with exponential backoff on disconnect
- Input validation for username and password on both frontend and backend
- Optional local Ollama AI assistant with
@AI,/ai, and/summary
- Go
- net/http
- github.com/gorilla/websocket
- golang.org/x/crypto/bcrypt
- Plain HTML/CSS/JavaScript
- Optional Ollama local model API
.
├── go.mod
├── go.sum
├── Dockerfile
├── docker-compose.yml
├── main.go
├── users.example.json
├── public/
│ ├── app.js
│ ├── index.html
│ ├── login.html
│ ├── register.html
│ ├── style.css
│ └── stickers/
│ ├── sticker-1.svg
│ ├── sticker-2.svg
│ ├── sticker-3.svg
│ ├── sticker-4.svg
│ ├── sticker-5.svg
│ └── sticker-6.svg
├── docs/
│ └── images/
│ ├── login.png
│ ├── chatroom.png
│ ├── reconnect.png
│ └── mobile.png
├── README.md
└── README_CN.md
Clean login interface with input validation for username (3-20 characters, letters/digits/hyphens/underscores) and password (minimum 6 characters).
Real-time multi-user chat with online user count, message history, emoji support, and local SVG stickers.
Automatic reconnection with exponential backoff when connection is lost. Shows connection status indicator (connected / disconnected / reconnecting).
Install Go 1.25+, then run:
go mod download
go run .Open:
http://localhost:8088
The app creates users.json automatically on first run. That file stores local demo users and is intentionally ignored by Git.
Build and run with Docker Compose:
docker compose up --buildOpen:
http://localhost:8088
The Compose file stores demo users in a named volume and sets optional Ollama environment variables. Ollama is not required; AI commands will show a friendly unavailable message if the Ollama service is not running.
To stop the containers:
docker compose downTo rebuild after code changes:
docker compose up --buildStart Ollama separately, then set the API URL and model if needed:
$env:OLLAMA_URL = "http://localhost:11434"
$env:OLLAMA_MODEL = "qwen2.5:3b"
go run main.goIn the chatroom:
@AI Explain WebSocket in one sentence
/ai Summarize what Gorilla WebSocket does
/summary
If Ollama is not running, the chatroom remains available and returns a friendly AI unavailable message.
- Zero external frontend dependencies: Pure HTML/CSS/JavaScript, no build tools required
- bcrypt password hashing: Passwords are never stored in plaintext
- HttpOnly + SameSite cookies: Session tokens are not accessible via JavaScript
- Message length limit: 800 characters per message with real-time counter
- Sticker URL whitelist: Only pre-approved sticker URLs are accepted
- Graceful AI fallback: Chatroom works perfectly without Ollama running
- Docker ready: One-command deployment with persistent user data volume
- WebSocket auto-reconnect: Automatic recovery from network interruptions
This project is intended for learning and local demos. It is not a production-ready chat system.
users.jsonis a simple local JSON file, not a database.- Sessions are stored in memory and disappear when the process restarts.
- The account system is intentionally minimal and lacks production features such as email verification, rate limiting, CSRF protection, audit logs, password reset, and persistent session storage.
- Do not expose this demo directly to the public internet without additional security hardening.
Run these read-only checks:
gofmt -d .
go mod tidy -diff
go test ./...
go vet ./...Verify the build separately:
go build ./...go build ./... writes a go-websocket-chatroom executable (or go-websocket-chatroom.exe on Windows) in the project root. The generated binary is ignored by Git and can be removed after verification. gofmt -w . and go mod tidy also update files; run them only when you intend to apply changes, then review the diff.
Manual checks:
- Visit
/while logged out and confirm it redirects to/login - Register and login with two accounts in separate browser windows
- Send text, emoji, and sticker messages
- Refresh the page and confirm recent history appears
- Try
@AIand/summarywith Ollama running - Stop Ollama and confirm AI commands show a friendly error
- Test username validation: try too short, too long, or special characters
- Test WebSocket disconnect: stop the server and observe reconnect behavior
- WebSocket auto-reconnect with exponential backoff
- Input validation for username and password
- Connection status indicator
- Add message timestamps by date group
- Add automated browser tests
- Add optional persistent database storage
- Add message search/filter
- Add user profile customization
MIT


