A Session is an extra session on a WebSocket connection, with its own authentication, namespace and database, and variables. db.Attach() and db.Fork() return one, and db.Sessions() lists them. Every query and CRUD function, such as Query and Select, takes a *Session in place of a *DB. See Multiple sessions for explanations and examples.
Sessions need a WebSocket connection. When the client reconnects, it attaches each session again under the same UUID and restores its state.
Source: session.go
Creating a session
db.Attach()
Attaches a new session to the connection. The new session starts signed out, with no namespace or database selected.
session, err := db.Attach(ctx)Returns: (*Session, error). On an HTTP connection, the error is constants.ErrSessionsNotSupported.
session, err := db.Attach(ctx)
if err != nil {
log.Fatal(err)
}
defer session.Detach(ctx)
if _, err := session.SignIn(ctx, surrealdb.Auth{Username: "root", Password: "secret"}); err != nil {
log.Fatal(err)
}
if err := session.Use(ctx, "surrealdb", "docs"); err != nil {
log.Fatal(err)
}
people, err := surrealdb.Query[[]Person](ctx, session, "SELECT * FROM person", nil) .Fork()
Attaches a new session that starts as a copy of this one. The new session has the same namespace, database and variables, and is signed in with the same access token. After that, the two sessions are independent. .Fork() does not copy a token pair.
fork, err := session.Fork(ctx)Returns: (*Session, error)
After the access token of the session expires, .Fork() returns an error that wraps constants.ErrForkTokenExpired. For a session that db.Sessions() lists but this client did not attach, it returns constants.ErrForkUntracked.
Properties
.ID()
Returns the UUID of the session.
id := session.ID()Returns: *models.UUID
Authentication methods
These methods work like the methods with the same name on DB, but only for this session.
.SignIn()
Signs a user in to this session.
token, err := session.SignIn(ctx, authData)Returns: (string, error)
.SignInWithRefresh()
Signs in through a TYPE RECORD access method defined WITH REFRESH, and returns an access token and a refresh token.
tokens, err := session.SignInWithRefresh(ctx, authData)Returns: (*Tokens, error)
.SignUp()
Signs up a record user, and signs them in to this session.
token, err := session.SignUp(ctx, authData)Returns: (string, error)
.SignUpWithRefresh()
Signs up through an access method defined WITH REFRESH, and returns an access token and a refresh token.
tokens, err := session.SignUpWithRefresh(ctx, authData)Returns: (*Tokens, error)
.Refresh()
Exchanges a token pair for a new one, and authenticates this session with it.
newTokens, err := session.Refresh(ctx, tokens)Returns: (*Tokens, error)
.Revoke()
Revokes the refresh token of a pair. The session stays signed in.
err := session.Revoke(ctx, tokens)Returns: error
.Tokens()
Returns the token pair that this session holds. It returns nil when the session holds no pair or when token renewal is off.
pair := session.Tokens()Returns: *Tokens
.Authenticate()
Authenticates this session with a JWT.
err := session.Authenticate(ctx, token)Returns: error
.Invalidate()
Signs this session out.
err := session.Invalidate(ctx)Returns: error
.Info()
Returns the record of the record user signed in to this session.
info, err := session.Info(ctx)Returns: (map[string]any, error)
Session state
.Use()
Selects the namespace and database of this session.
err := session.Use(ctx, ns, database)Returns: error
.Let()
Sets a variable in this session. The client keeps a copy of the variable, encoded once. It uses this copy to set the variable again when it reconnects, and to copy the variable to a fork.
err := session.Let(ctx, key, val)Returns: error
.Unset()
Removes a variable from this session.
err := session.Unset(ctx, key)Returns: error
.Reset()
Returns this session to the state it had when the client attached it. It signs the session out, clears its namespace, database and variables, and kills its live queries. The session stays attached.
err := session.Reset(ctx)Returns: error
.Version()
Returns the version of the server.
ver, err := session.Version(ctx)Returns: (*VersionData, error)
Transactions and live queries
.Begin()
Starts an interactive transaction in this session.
tx, err := session.Begin(ctx)Returns: (*Transaction, error). See Transaction. After .Detach(), the error is constants.ErrSessionClosed.
.LiveNotifications()
Returns the notification channel of a live query that this session started with Live.
ch, err := session.LiveNotifications(liveQueryID)Returns: (chan connection.Notification, error)
.CloseLiveNotifications()
Closes the notification channel of a live query, without stopping the live query on the server.
err := session.CloseLiveNotifications(liveQueryID)Returns: error
Closing a session
.Detach()
Removes the session from the server and closes the channels of its live queries. After .Detach(), you cannot use the session, and its methods return constants.ErrSessionClosed.
err := session.Detach(ctx)Returns: error
See also
DB for the client and the query functions
Transaction for transactions in a session
Multiple sessions for session usage patterns
Errors for
ErrSessionClosedand the other session errors