Documentation Index
Fetch the complete documentation index at: https://docs.go-mizu.dev/llms.txt
Use this file to discover all available pages before exploring further.
The live package provides low-latency real-time message delivery over WebSocket with topic-based publish/subscribe. Itβs designed as a transport layer for instant updates: chat, notifications, collaborative editing, real-time dashboards, and more.
What is Real-time Communication?
In traditional HTTP, the browser sends a request and waits for a response. For new data, you must make another request (polling).
Traditional HTTP:
Client: "Give me messages" β Server: Returns messages
Client: (waits...)
Client: "Any new messages?" β Server: Returns messages
With WebSockets, the connection stays open. Either side can send messages anytime.
WebSocket:
Client: "Connect" β Server: Connected!
Server: "New message!" β Client: Receives instantly
Server: "Another message!" β Client: Receives instantly
Client: "I'm typing..." β Server: Receives instantly
Design Principles
| Principle | Description |
|---|
| Transport-only | Moves messages, doesnβt interpret state |
| Best-effort | No durability or replay guarantees |
| Topic-based | Scalable, simple pub/sub routing |
| Opaque payloads | Your app defines message schemas |
| Minimal surface | Few types, predictable behavior |
Architecture
βββββββββββββββββββββββ
β Live Server β
β β
Clients β βββββββββββββββββ β
β β β Sessions β β
β WebSocket β β β β
ββββββββββββββββββββββββΆβ β Session A β β
β β β Session B β β
β WebSocket β β Session C β β
ββββββββββββββββββββββββΆβ β β β
β β βββββββββββββββββ β
β WebSocket β β β
ββββββββββββββββββββββββΆβ β β
β β ββββββββΌβββββββ β
β β Topics β β
β β β β
β β "chat:room1"β β
β β "user:123" β β
β β "global" β β
β β β β
β βββββββββββββββ β
β β
βββββββββββββββββββββββ
- Clients connect via WebSocket
- Sessions created for each connection
- Server subscribes sessions to topics
- Messages published to topics
- All subscribers receive the message
Core Concepts
The Server manages WebSocket connections and pub/sub routing.
import "github.com/go-mizu/mizu/live"
server := live.New(live.Options{
OnAuth: func(ctx context.Context, r *http.Request) (any, error) {
// Authenticate connection
return userID, nil
},
})
Session
A Session represents one WebSocket connection. Each has:
- Unique ID
- Optional value from authentication
- Send queue for outgoing messages
Message
Messages have a topic and data:
type Message struct {
Topic string `json:"topic,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
}
Topics are named channels. Sessions subscribe to topics they care about. When you publish to a topic, all subscribers receive the message.
// Subscribe session to topic
server.Subscribe(session, "chat:room1")
// Publish to all subscribers
server.Publish("chat:room1", []byte(`{"text":"Hello!"}`))
Quick Example
Server (Go):
package main
import (
"context"
"encoding/json"
"net/http"
"github.com/go-mizu/mizu"
"github.com/go-mizu/mizu/live"
)
func main() {
server := live.New(live.Options{
OnAuth: func(ctx context.Context, r *http.Request) (any, error) {
userID := r.URL.Query().Get("user")
if userID == "" {
return nil, fmt.Errorf("user required")
}
return userID, nil
},
OnMessage: func(ctx context.Context, s *live.Session, topic string, data []byte) {
// Handle incoming messages
if topic == "subscribe" {
var req struct{ Topic string `json:"topic"` }
json.Unmarshal(data, &req)
server.Subscribe(s, req.Topic)
} else if topic == "chat" {
// Broadcast to all in chat
server.Publish("chat:general", data)
}
},
OnClose: func(s *live.Session, err error) {
log.Printf("Session %s closed: %v", s.ID(), err)
},
})
app := mizu.New()
app.Get("/ws", mizu.Compat(server.Handler()))
app.Listen(":8080")
}
Client (JavaScript):
const ws = new WebSocket('ws://localhost:8080/ws?user=alice');
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
console.log(`[${msg.topic}]`, msg.data);
};
// Subscribe to a topic
ws.send(JSON.stringify({topic: 'subscribe', data: {topic: 'chat:general'}}));
// Send a chat message
ws.send(JSON.stringify({topic: 'chat', data: {text: 'Hello!'}}));
When to Use Live
Use Live when:
- You need instant updates (< 100ms latency)
- Multiple clients need to see the same data
- Building interactive features (chat, gaming)
- Pushing data to clients (notifications)
Consider alternatives when:
- Updates are infrequent (use polling or SSE)
- You need message durability (use sync package)
- Building a simple REST API
Whatβs Next?
- Quick Start - Build your first live feature
- Server - All server options explained
- Sessions - Connection lifecycle
- Pub/Sub - Topic patterns and publishing