Edit: I'm no longer trying to do this. Now I just use the surrealdb rust client with the protocol-ws feature flag.
I'm connecting to surreal db's /sql
endpoint with a websocket.
The http docs say to NS
and DB
http headers, but my WebSocket library doesn't seem to support this.
I'd like to just send a USE statement but it seems to get ignored.
It returns the reply: [{"time":"7.383µs","status":"OK","result":null}]
Then as soon as I try a select statement I get: [{"time":"13.028µs","status":"ERR","detail":"Specify a namespace to use"}]
Surreal logs show its executing both statements:
[2022-10-10 00:12:21] INFO surreal::web 127.0.0.1:46148 GET /sql HTTP/1.1 101 "-" 47.774µs
[2022-10-10 00:12:21] DEBUG surrealdb::dbs Executing: USE NS webapp DB webapp
[2022-10-10 00:12:21] DEBUG surrealdb::dbs Executing: SELECT * FROM user
Please firstly know that SurrealDB is beta thus encounter with errors. One endpoint didn't reflect all subordinates of endpoint as such websocket so we needed to add OpenLiteSpeed one by one:
let surrealhere = "https://yourwebsite.com"
async function main() {
let username = 'yourusername';
let password = 'yourpass';
let auth = btoa(`${username}:${password}`);
try {
// const doit = async () =>{
let res = await fetch( `${surrealhere}/key/article` ,{
method : "get",
headers: {
'Authorization': `Basic ${auth}`,
'Accept': 'application/json',
'Content-Type' : 'application/json',
'NS':'my',
'DB':'my'
},
})
results =await res.json()
if (results.length > 0) {
results = results[0].result
}
// results = JSON.stringify(results)
console.log( "xxxxxxxxxxxxxxxxxxxxx" , results )
} catch (e) {
console.error('ERROR', e);
}
}
Notice that we needed to specify each endpoints from the main website let surrealhere = "https://yourwebsite.com"
instead of a subpage like let surrealhere = "https://yourwebsite.com/surreal"
so that websocket is not confused in terms of opening and closing from different subpages.