Skip to content

API Reference

/

Core

Session

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

Attaches a new session to the connection. The new session starts signed out, with no namespace or database selected.

Syntax
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)

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.

Syntax
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.

Returns the UUID of the session.

Syntax
id := session.ID()

Returns: *models.UUID

These methods work like the methods with the same name on DB, but only for this session.

Signs a user in to this session.

Syntax
token, err := session.SignIn(ctx, authData)

Returns: (string, error)

Signs in through a TYPE RECORD access method defined WITH REFRESH, and returns an access token and a refresh token.

Syntax
tokens, err := session.SignInWithRefresh(ctx, authData)

Returns: (*Tokens, error)

Signs up a record user, and signs them in to this session.

Syntax
token, err := session.SignUp(ctx, authData)

Returns: (string, error)

Signs up through an access method defined WITH REFRESH, and returns an access token and a refresh token.

Syntax
tokens, err := session.SignUpWithRefresh(ctx, authData)

Returns: (*Tokens, error)

Exchanges a token pair for a new one, and authenticates this session with it.

Syntax
newTokens, err := session.Refresh(ctx, tokens)

Returns: (*Tokens, error)

Revokes the refresh token of a pair. The session stays signed in.

Syntax
err := session.Revoke(ctx, tokens)

Returns: error

Returns the token pair that this session holds. It returns nil when the session holds no pair or when token renewal is off.

Syntax
pair := session.Tokens()

Returns: *Tokens

Authenticates this session with a JWT.

Syntax
err := session.Authenticate(ctx, token)

Returns: error

Signs this session out.

Syntax
err := session.Invalidate(ctx)

Returns: error

Returns the record of the record user signed in to this session.

Syntax
info, err := session.Info(ctx)

Returns: (map[string]any, error)

Selects the namespace and database of this session.

Syntax
err := session.Use(ctx, ns, database)

Returns: error

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.

Syntax
err := session.Let(ctx, key, val)

Returns: error

Removes a variable from this session.

Syntax
err := session.Unset(ctx, key)

Returns: error

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.

Syntax
err := session.Reset(ctx)

Returns: error

Returns the version of the server.

Syntax
ver, err := session.Version(ctx)

Returns: (*VersionData, error)

Starts an interactive transaction in this session.

Syntax
tx, err := session.Begin(ctx)

Returns: (*Transaction, error). See Transaction. After .Detach(), the error is constants.ErrSessionClosed.

Returns the notification channel of a live query that this session started with Live.

Syntax
ch, err := session.LiveNotifications(liveQueryID)

Returns: (chan connection.Notification, error)

Closes the notification channel of a live query, without stopping the live query on the server.

Syntax
err := session.CloseLiveNotifications(liveQueryID)

Returns: error

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.

Syntax
err := session.Detach(ctx)

Returns: error

  • DB for the client and the query functions

  • Transaction for transactions in a session

  • Multiple sessions for session usage patterns

  • Errors for ErrSessionClosed and the other session errors

Was this page helpful?

Last updated