razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
301 stars 9 forks source link

How to use with a database subscription? #16

Closed mcavaciocchi closed 10 months ago

mcavaciocchi commented 10 months ago

It's possible to return events when they happen in a database subscription? I'm using postgres js library.

export async function GET() {
    const { unsubscribe } = await sql.subscribe(
        '*:msg',
        (row, { command }) => {
            console.log(command)
        },
        () => {
            console.log('connect')
        }
    )
    return event(async function run(emit) {
        emit(`${Date.now()}`)
    }).toResponse()
}
razshare commented 10 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.

mcavaciocchi commented 10 months ago

This was exactly what I needed!! Thx