There are two different connection engines you can use to connect to SurrealDb backend. You can do so via Websocket or through HTTP connections
db, err := surrealdb.New("ws://localhost:8000")
or for a secure connection
db, err := surrealdb.New("wss://localhost:8000")
There are some functions that are not available on RPC when using HTTP but on Websocket. All these except the “live” endpoint are effectively implemented in the HTTP library and provides the same result as though it is natively available on HTTP. While using the HTTP connection engine, note that live queries will still use a websocket connection if the backend supports it
db, err := surrealdb.New("http://localhost:8000")
or for a secure connection
db, err := surrealdb.New("https://localhost:8000")
The connection engine you choose affects how authentication tokens and sessions work:
With websockets connections (ws://
, wss://
) you open a single long-lived stateful connection where after the initial authentication, the session duration applies and if not specified, defaults to NONE
meaning that the session never expires unless otherwise specified.
When you connect with a HTTP connection (http://
, https://
), every request you make is short-lived and stateless, requiring you to authenticate every request individually for which the token is used, creating a short lived session. Hence, the token duration which defaults to 1 hour applies.
You can extend the session duration of a token or a session by setting the DURATION
clause when creating a new access method with the DEFINE ACCESS METHOD
statement or when defining a new user with the DEFINE USER
statement.
Learn more about token and session duration in our security best practices documentation.