Skip to main content
The Live server manages WebSocket connections, handles message routing, and provides hooks for authentication and lifecycle events.

Creating a Server

Configuration Options

OnAuth

Called when a client attempts to connect. Return a value to store in the session, or an error to reject.
If OnAuth is nil, all connections are accepted.

OnMessage

Called when a client sends a message. The message is parsed and topic/data are extracted.
Client messages must be JSON with topic and data:

OnClose

Called when a connection closes.

QueueSize

Per-session send buffer size. When full, the session is closed.

ReadLimit

Maximum incoming message size in bytes.

Origins

List of allowed WebSocket origins.
Use "*" to allow all origins (not recommended for production).

CheckOrigin

Custom origin validation (takes precedence over Origins).

IDGenerator

Custom session ID generator.

Server Methods

Handler

Returns an HTTP handler for WebSocket upgrades.

Publish

Sends data to all sessions subscribed to a topic.

Subscribe

Subscribes a session to a topic.

Unsubscribe

Removes a session from a topic.

Complete Example

Mounting the Handler

With Mizu

With Standard Library

Best Practices

1. Always Authenticate

2. Set Reasonable Limits

3. Handle Close Gracefully

4. Validate Subscriptions