IHP 1.3 is out now!

Autorefresh as pubsub

Aksel Stadler Kjetså

I set up a websocket connection using this example https://ihp.digitallyinduced.com/Guide/websockets.html

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? https://ihp.digitallyinduced.com/Guide/realtime-spas.html

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: https://github.com/digitallyinduced/ihp/blob/master/IHP/DataSync/Controller.hs#L76

You need to do two things:

  1. Set up a notification trigger, e.g. like here https://github.com/digitallyinduced/ihp/blob/master/IHP/DataSync/Controller.hs#L77
  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). https://github.com/digitallyinduced/ihp/blob/master/IHP/DataSync/Controller.hs#L114

AutoRefresh also uses the PGListener API.