appy-one / acebase-client

Client to connect to remote AceBase NoSQL database server
MIT License
21 stars 8 forks source link

Run query from cloud function / serverless function? Disable websocket? #18

Closed qpwo closed 2 years ago

qpwo commented 2 years ago

I am trying to get acebase-client to work in a serverless function. Do you have a recommended way to do this, or do you recommend against it?

The below snippet works fine in the browser and in straight node, but not in my local development version of next.js's/vercel's serverless function API thing:

const db = new AceBaseClient({
    host: 'localhost',
    port: 5757,
    dbname: 'mydb',
    https: false,
})
await db.ready()
await db.auth.signIn('admin', 'coolpassword')
await db.ref('counter').transaction(snap => snap.val() + 1 )
console.log('final value:' ,(await db.ref('counter').get()).val())

Specifically, db.ready() never completes and I get a bunch of Websocket connection error: Error: websocket error.

I tried network: { transports: ['polling'] }, and sync: { timing: 'manual' }, in the new AceBaseClient, but neither seemed to help. I also tested if an axios.get() from within a cloud function worked (in case it was some kind of CORS problem) and it executed fine.

appy-one commented 2 years ago

Vercel serverless functions don't support websocket connections. See https://vercel.com/support/articles/do-vercel-serverless-functions-support-websocket-connections .

I'll look at implementing a switch that disables the websocket connection. Doing so would disable getting real-time notifications, but still allow you to query and update the db. Would that be a solution for you?

qpwo commented 2 years ago

Yes! That's exactly what I'm looking for!

qpwo commented 2 years ago

I think it could also be useful for low-bandwidth / intermittent connections and scripts or CLI tools, where you may just want a quick query

qpwo commented 2 years ago

How challenging do you think this would be? I may be able to help some, although not very quickly

appy-one commented 2 years ago

I'm afraid it's not a very simple procedure but I will definitely look into it

appy-one commented 2 years ago

I've added a realtime network setting that allows disabling the websocket connection and only use the REST API. Can you try updating acebase-client to v1.12.0, then add network: { realtime: false } to your client settings. This will cause a ping to the server every 60 seconds to allow connection state to keep working, You can disable that too with the monitor setting, but that will probably cause issues as I haven't tested that combination yet.

Full example:

const db = new AceBaseClient({ dbname: 'mydb', host: 'localhost', port: 1352, network: { realtime: false, monitor: true, interval: 60 })

Let me know if that works!

appy-one commented 2 years ago

I'm closing this for now, let me know if this is working. Feel free to reopen if not.