Closed mcavaciocchi closed 11 months ago
I'm not very familiar with db subscriptions, but this should work
import { event } from 'sveltekit-sse'
export async function GET() {
/**
* @type {Array<function(...any):any>}
*/
let unsubscribes = []
/**
* @type {false|function(any):void}
*/
let stop = false
function release() {
for (const unsubscribe of unsubscribes) {
unsubscribe()
}
if (!stop) {
return
}
stop(false)
}
function lock() {
return new Promise(function run(stopLocal) {
stop = stopLocal
// when you invoke "stop", the promise will resolve,
// releasing the lock at line 50 and ending the stream.
})
}
/**
* @param {import("sveltekit-sse").EmitterOfOneEvent} emit
*/
async function provide(emit) {
const { unsubscribe } = await sql.subscribe(
'*:msg',
function message(row, { command }) {
console.log(command)
emit('...')
},
function connect() {
console.log('connect')
},
)
unsubscribes.push(unsubscribe)
}
return event(async function run(emit) {
await provide(emit)
await lock()
})
.onCancel(release)
.toResponse()
}
This code will probably not work for you right out of the gate because I'm not familiar with the api of whatever library you're using.
So you'll need to make some adjustments yourself.
The idea is that you only invoke release()
whenever you want to end the stream.
As long as you don't invoke release()
or there's some error, the stream will stay online.
This was exactly what I needed!! Thx
It's possible to return events when they happen in a database subscription? I'm using postgres js library.