A session wraps a WebSocket connection with an isolated session ID. Each session maintains its own namespace, database, variables, and authentication state — independent of other sessions on the same connection.
Sessions are created by calling .new_session() on a WebSocket connection. They are not available on HTTP or embedded connections.
Note
Sessions require a WebSocket connection (ws:// or wss://). Attempting to create a session on an HTTP or embedded connection raises UnsupportedFeatureError.
A session exposes the same interface as the parent connection. All methods below delegate to the underlying connection, scoped to this session's ID. For full parameter tables and examples, see the Surreal reference.
Closes this session on the server, releasing its session ID and any associated state. After calling this method, the session object should not be used.
# Session A works with the "shop" namespace session_a=db.new_session() session_a.use("shop","inventory") session_a.create("products",{"name": "Laptop","price": 999.99})
# Session B works with a different namespace independently session_b=db.new_session() session_b.use("analytics","events") session_b.create("page_views",{"page": "/products","count": 1})
# Each session has its own authentication state session_a.signin({"username": "root","password": "root"}) products=session_a.select("products") print("Products:",products)
# Transactions within a session txn=session_a.begin_transaction() txn.create("products",{"name": "Mouse","price": 29.99}) txn.create("products",{"name": "Keyboard","price": 59.99}) txn.commit()