Session
The Session struct represents an additional SurrealDB session on a WebSocket connection. Each session has its own authentication state, namespace and database selection, and connection variables. Sessions require SurrealDB v3+ and a WebSocket connection.
Session satisfies the sendable constraint, so all generic functions like Query, Select, Create, etc. accept *Session directly.
Source: session.go
Creating a Session
db.Attach()
Creates a new session on the WebSocket connection. The session starts unauthenticated and without a selected namespace or database.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
Returns: (*Session, error)
Returns ErrSessionsNotSupported if the connection is not WebSocket.
Examples
Properties
.ID()
Returns the session's UUID.
Returns: *models.UUID
Methods
.Detach()
Removes the session from the server. After calling .Detach(), the session cannot be used.
Returns: error
Returns ErrSessionClosed if the session has already been detached.
.Begin()
Starts a new interactive transaction within this session.
Returns: (*Transaction, error) — see Transaction
Returns ErrSessionClosed if the session has been detached.
.SignIn()
Signs in an existing user within this session.
Returns: (string, error)
.SignInWithRefresh()
Signs in with refresh token support within this session. SurrealDB v3+ only.
Returns: (*Tokens, error)
.SignUp()
Signs up a new record user within this session.
Returns: (string, error)
.SignUpWithRefresh()
Signs up with refresh token support within this session. SurrealDB v3+ only.
Returns: (*Tokens, error)
.Authenticate()
Authenticates this session with a JWT token.
Returns: error
.Invalidate()
Invalidates the current authentication for this session.
Returns: error
.Use()
Selects the namespace and database for this session.
Returns: error
.Let()
Defines a variable scoped to this session.
Returns: error
.Unset()
Removes a variable from this session.
Returns: error
.Info()
Returns the record of the currently authenticated user in this session.
Returns: (map[string]any, error)
.Version()
Returns the SurrealDB version information.
Returns: (*VersionData, error)
.LiveNotifications()
Returns the notification channel for a live query.
Returns: (chan connection.Notification, error)
.CloseLiveNotifications()
Closes the notification channel for a live query.
Returns: error
See Also
DB for the main client reference
Transaction for session-scoped transactions
Multiple sessions for session usage patterns
Errors for
ErrSessionClosedandErrSessionsNotSupported