Skip to main content
A Session represents a single WebSocket connection. Each connected client has its own session with a unique ID and associated value from authentication.

Session Lifecycle

Session Methods

ID

Returns the session’s unique identifier.

Value

Returns the value stored during authentication.

Send

Queues a message for delivery to the client. Non-blocking.

Close

Closes the connection gracefully.

IsClosed

Checks if the session is closed.

CloseError

Returns the error that caused the session to close.

Subscription Management

Subscriptions are managed through the server, not the session:
When a session closes, it’s automatically unsubscribed from all topics.

Common Patterns

User Authentication

Auto-Subscribe on Connect

Since there’s no OnConnect hook, use OnMessage with a special “init” topic:
Client sends init on connect:

Presence Tracking

Direct Messaging

Room Management

Errors

Best Practices

1. Store Minimal Data in Value

2. Handle Close Gracefully

3. Check Before Sending

4. Handle Reconnection

Clients will reconnect. Design your protocol to handle it: