Skip to content

About

Go WebSocket 实时聊天室学习项目:登录会话、消息广播、历史消息、emoji 与可选本地 Ollama 助手。

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Go WebSocket Chatroom Demo

中文说明

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.

Features

  • 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

Tech Stack

  • Go
  • net/http
  • github.com/gorilla/websocket
  • golang.org/x/crypto/bcrypt
  • Plain HTML/CSS/JavaScript
  • Optional Ollama local model API

Directory Structure

.
├── 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

Screenshots

Login Page

Login Page

Clean login interface with input validation for username (3-20 characters, letters/digits/hyphens/underscores) and password (minimum 6 characters).

Chatroom

Chatroom

Real-time multi-user chat with online user count, message history, emoji support, and local SVG stickers.

WebSocket Reconnect

WebSocket Reconnect

Automatic reconnection with exponential backoff when connection is lost. Shows connection status indicator (connected / disconnected / reconnecting).

Getting Started

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.

Docker

Build and run with Docker Compose:

docker compose up --build

Open:

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 down

To rebuild after code changes:

docker compose up --build

Optional Ollama AI Assistant

Start 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.go

In 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.

Project Highlights

  • 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

Security Notice

This project is intended for learning and local demos. It is not a production-ready chat system.

  • users.json is 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.

Testing

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 @AI and /summary with 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

Roadmap

  • 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

License

MIT

About

Go WebSocket 实时聊天室学习项目:登录会话、消息广播、历史消息、emoji 与可选本地 Ollama 助手。

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages