Need help using websockets in js

I’m not sure how to open a websocket connection, particularly in terms of authorizing it. I tried following the process used in the pyspeckle client (i.e. make a post request to ‘/accounts/login’ with credentials, then receive a token), but I get “Invalid credentials.” back.

Code looks like this:

    fetch(`https://${host}/api/accounts/login`, {
    method: "POST",
    body: JSON.stringify({
        'email': email,
        'password': password
    })
    })
    .then(response => {
        return response.json();
    })
    .then( json => {
        console.log(json)
    })

email and password are working when login into the webapp

tested in python with the same credentials and it also worked:

r = requests.post(
'https://hestia.speckle.works/api/accounts/login',
json={
    'email': email,
    'password': password
})
response = r.json()
token = r.json()['resource']['token']

Hey @tluther! I originally misinterpreted your question :grimacing: A wee bit confused - code you posted should work for getting your account token if local auth strategies are enabled on the server (currently most of them are).

Here’s how we deal with websockets in the browser: https://github.com/speckleworks/SpeckleAdmin/blob/master/src/components/ViewerLoadedStreamsCard.vue#L48

I’ve also spotted a bug in the code linked there; I’m not authorising the connection with the token :confused: (this means that i’m not subscribing to events on private streams) to actually do that, the correct connection url should be:

this.ws = new sockette( `${wsUrl}?client_id=${this.$store.state.client._id}&access_token=${your_token}`, {

I’m using sockette, quite a nifty websocket lib that makes life quite a bit easier. Also the cutest js lib around probably.

PS: I found myself using https://www.websocket.org/echo.html to test things some time ago…

Hmm… I’m just using hestia.speckle.works at the moment, and the python code worked so really not sure why it isn’t working in JS…

Can you think of anything that might lead to the “Invalid credentials.” message being returned even when the credentials are correct?