surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
272 stars 45 forks source link

Bug: Connection Unavailable with HTTP protocol in v1.0.0-beta.5 #245

Closed dodanex closed 2 months ago

dodanex commented 2 months ago

Describe the bug

Using v1.0.0-beta.5 I got this error: ConnectionUnavailable: There is no connection available at this moment.

But after switching back to v0.11.1 it works fine.

Edit from maintainer: In the previous SDK, the class you used decided which protocol you would connect over, meaning http would get "corrected" to websocket. There seems to be an issue with the HTTP engine, need to investigate what is going on there.

Steps to reproduce

Use the v1.0.0-beta.5 and try to connect to the database.

Expected behaviour

To work as expected.

SurrealDB version

1.4.2

SurrealDB.js version

1.0.0-beta.5

Is there an existing issue for this?

Code of Conduct

gtwww commented 2 months ago

The Nuxt project was working fine under surrealdb.js@0.11.1, but when I upgraded to the latest surrealdb.js@1.0.0-beta.5, I encountered an issue. The error message is as follows:

dev:  ERROR  [nitro] [unhandledRejection] There is no connection available at this moment.
dev:
dev:   at HttpEngine.rpc (node_modules/.pnpm/surrealdb.js@1.0.0-beta.5/node_modules/surrealdb.js/src/library/engine.ts:315:10)
dev:   at runNextTicks (node:internal/process/task_queues:60:5)
dev:   at listOnTimeout (node:internal/timers:540:9)
dev:   at process.processTimers (node:internal/timers:514:7)
dev:   at Surreal.ping (node_modules/.pnpm/surrealdb.js@1.0.0-beta.5/node_modules/surrealdb.js/src/surreal.ts:160:21)

dev:  ERROR  [nitro] [unhandledRejection] There is no connection available at this moment.
dev:
dev:   at HttpEngine.rpc (node_modules/.pnpm/surrealdb.js@1.0.0-beta.5/node_modules/surrealdb.js/src/library/engine.ts:315:10)
dev:   at Surreal.ping (node_modules/.pnpm/surrealdb.js@1.0.0-beta.5/node_modules/surrealdb.js/src/surreal.ts:160:21)

I noticed that the client connection method in the documentation remains the same. I'm not sure if it's a compatibility issue with other dependencies.

Steps to reproduce pnpm update surrealdb.js

Expected behaviour The project was working fine under surrealdb.js@1.0.0-beta.5

kearfy commented 2 months ago

Hey @dodanex and @gtwww, could you both share how you initiate the connection to SurrealDB with the library? I'll see if I'm able to reproduce this issue

dodanex commented 2 months ago

@kearfy I just followed the documentation to connect to SurrealDB. I'm also using Nuxt.js v3. Keep in mind that the same code is working with v0.11.1 but not in beta5!

import { Surreal } from 'surrealdb.js'

export const db = new Surreal()

export default defineNitroPlugin(async () => {
    try {
        const config = useRuntimeConfig()
        await db.connect(config.DB_URL)
        await db.signin({
            username: config.DB_USER,
            password: config.DB_PASSWORD,
            namespace: config.DB_NAMESPACE,
            database: config.DB_NAME
        })

        console.info('<===[ SurrealDB is Connected! ]===>')
    } catch (error) {
        throw new Error(error as unknown as string)
    }
})
gtwww commented 2 months ago

@kearfy @dodanex Got it! Changing the connection url from http://localhost:8000/rpc to ws://localhost:8000 it's working fine now.

dodanex commented 2 months ago

@kearfy I confirm that changing from http to ws I'm now able to connect to the database. Thank you!

kearfy commented 2 months ago

Ah! It should still work via HTTP aswell however 🤔 I'll reopen and adjust the title for the moment, need to investigate why that is happening... Thanks!