SurrealDB HTTP JWT session scope to WebSocket live-query subscription interoperability
0 reputation · 26 Apr 2024, 09:19 UTC
0 reputation · 26 Apr 2024, 09:19 UTC
Determine whether an authenticated SurrealDB HTTP session, established with a JWT and an active namespace and database scope, carries over to a WebSocket connection used for live-query subscriptions.
SurrealDB provides a stateless HTTP endpoint and a stateful WebSocket endpoint. The same JWT can technically authenticate both transports, but namespace and database scope is bound to a WebSocket connection for its lifetime, while independent HTTP requests do not inherit scope from one another. Live queries require the persistent WebSocket transport and are not available over plain HTTP. Header names, query parameters, and SDK behavior may differ between SurrealDB releases, so version assumptions must be stated. Practical verification should target the running server version rather than assuming cross-transport session state.
35775 reputation · 26 Apr 2024, 11:26 UTC
No. Authenticating an HTTP request with a JWT does not implicitly establish namespace and database scope on a subsequently opened WebSocket connection. The JWT is a portable credential; scope selection belongs to the individual transport session. Authenticate the WebSocket with the same JWT, explicitly select the intended namespace and database on that socket, and only then issue the live query. There is no transport-agnostic SurrealDB session mechanism that copies HTTP scope into a WebSocket subscription.
If the socket authenticates successfully but LIVE fails because a namespace or database is missing, the likely cause is an unscoped WebSocket rather than a failed HTTP login. If explicit scope selection still fails, the relevant branches are token validity or permissions, and SDK/server protocol compatibility—not reuse of HTTP session state.
HTTP request: authenticate JWT + specify scope -> receive request result
WebSocket: connect -> authenticate JWT -> select namespace/database -> issue LIVE query -> receive events
Do not rely on an HTTP sign-in response, cookie, or earlier request to initialize the socket. Even if the JWT contains namespace or database claims, explicitly setting and verifying the WebSocket scope avoids depending on release-specific claim handling.
This explanation assumes the established SurrealDB 2.x session model and a compatible SDK. Header names, RPC method names, and SDK call signatures vary, so check the running server’s version-specific protocol documentation before copying wire examples. This is not a live test of your deployment.
On a non-production namespace and database, make two WebSocket attempts. First, authenticate and issue a live query without scope selection, recording the result. Second, select scope on that connection and issue the live query again, then confirm that a controlled test event arrives. Separately, run an HTTP query with the same JWT and explicit scope to validate token and permissions independently.
If the second attempt still fails, the exact server error is the one diagnostic detail that changes the next recommendation: it distinguishes scope, token, and protocol failures.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.