Multiple sessions
Sessions allow you to create isolated contexts on a single WebSocket connection. Each session has its own authentication state, namespace and database selection, and connection variables. This is useful when a single application needs to serve multiple users or tenants over one connection.
Sessions require a WebSocket connection (ws:// or wss://) and SurrealDB v3 or later.
API References
| Method | Description |
|---|---|
db.Attach(ctx) | Creates a new session on the connection |
session.Detach(ctx) | Removes the session from the server |
session.ID() | Returns the session's UUID |
Creating a session
Call .Attach() on the *DB to create a new session. The session starts unauthenticated and without a selected namespace or database, so you must configure it before making queries.
Querying with a session
Sessions satisfy the sendable constraint, so all generic functions like Query, Select, Create, etc. accept a *Session directly.
Each session maintains its own state. Changes to variables, authentication, or namespace on one session do not affect the parent *DB or other sessions.
Session isolation
Sessions are fully isolated from each other and from the parent connection:
Authentication is independent: signing in on a session does not affect other sessions or the
*DB..Use()on a session does not change the namespace/database of other sessions.Variables set with
.Let()are scoped to the session.Live queries started on a session are scoped to that session.
Starting transactions from a session
Sessions can start their own transactions using .Begin(). The transaction inherits the session's authentication and namespace context.
See Transactions for more on interactive transactions.
Detaching a session
Call .Detach() to remove the session from the server. After detaching, the session cannot be used and any operations on it return ErrSessionClosed.
Use defer session.Detach(ctx) immediately after .Attach() to ensure cleanup.
Learn more
Session API reference for complete method signatures
Transactions for interactive transaction support
Connecting to SurrealDB for WebSocket connection requirements
Errors reference for session-related error types