2

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
matiu
  • 7,469
  • 4
  • 44
  • 48
  • can you show the code where you execute the statements? Have you tried running both statemenets in a single query? – Tobias S. Oct 09 '22 at 16:32

3 Answers3

2

this answer helped get me moving.

I had to do the following:

  1. Define a scope that I could log into on the DB
  2. Create a record in that scope
  3. Open the websocket to ws://localhost:8000/rpc (I was opening it to /ws)
  4. Create an JSON-RPC like request

Define a scope

DEFINE SCOPE admin SESSION 1d
        SIGNUP ( CREATE user SET user = $user, pass = crypto::argon2::generate($pass) )
        SIGNIN ( SELECT * FROM user WHERE user = $user AND crypto::argon2::compare(pass, $pass));

Create a user in that scope

create user:matiu SET user = "matiu", pass = crypto::argon2::generate("matiu");

Connect the websocket

use gloo_net::websocket::{futures::WebSocket, Message};
let ws = WebSocket::open("ws://localhost:8080/rpc").unwrap();

Send this json on the websocket

{"id":"1","method":"signin","params":[{"NS":"webapp","DB":"webapp","SC":"admin","user":"matiu","pass":"matiu"}]}

Now I have the trouble that my select queries return no data :/ I probably have to modify the scope permissions or something. I'll update here once I figure that out.

matiu
  • 7,469
  • 4
  • 44
  • 48
  • Is there any doc for the different methods that exist? I've managed to use "signin" but I can't get anything else to work. – Wolfeur Jun 12 '23 at 14:47
1

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.

Subpages

Websocket

Bitfinicon
  • 1,045
  • 1
  • 8
  • 22
0

You can try to combine "USE NS webapp DB webapp" and "SELECT * FROM user" into one line of code.

Tiansi Liu
  • 41
  • 2
  • 1
    You get a syntax error. Both with and wthout a ';' between the statements. I also tried with a newline betwteen the staments. Syntax error. – matiu Oct 15 '22 at 04:34