Autorefresh as pubsub

Aksel Stadler Kjetså

I set up a websocket connection using this example

Would it be possible to track certain DB tables here as well? Like AutoRefresh is doing for actions. I want to push something over WS when a table is updates/inserted. I understand that is what autorefresh is doing, but i'm trying to use this as a backend to backend communication, and autorefresh seems to require an action

Lillo PRO

Maybe DataSync could be for you?

It lets you subscribe to certain database tables like for example this through JavaScript:

function callback(todos) {
    console.log('todos did change', todos);

const todos = await query('todos').fetchAndRefresh(callback);
Aksel Stadler Kjetså

Thanks! I'll check it out :) will update if i make it work!

Aksel Stadler Kjetså

Interestingly the documentations shows how to listen for changes from javascript, but not how to do it from within Haskell. So it is definitely possible, i just need to find the haskell side code of the ws connection

marc PRO

You can find the DB watcher code for DataSync starting here:

You need to do two things:

  1. Set up a notification trigger, e.g. like here
  2. Get a pgListener instance from the application context and then call subscribe or subscribeJSON (this automatically decodes the response the postgres event as json).

AutoRefresh also uses the PGListener API.