Hello all,
I have a scenario where both Wappler and other services (written in Python & Java) write to the same Postgres database.
In this scenario I'd love for Wappler to know when the database is updated via the other services and to then push real-time status updates to the app UI using the sockets capability in Wappler.
There's a Postgres feature called triggers that can be used to notify a Node.js function when selected tables are updated. The SQL code for creating the trigger function looks like this:
CREATE OR REPLACE FUNCTION notify_change()
RETURNS trigger AS $$
DECLARE
payload JSON;
BEGIN
payload = json_build_object(
'table', TG_TABLE_NAME,
'operation', TG_OP,
'data', row_to_json(NEW)
);
PERFORM pg_notify('table_update', payload::text);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
You then attach the trigger to specific actions on a table (the notifications table in this case):
CREATE TRIGGER user_update_trigger
AFTER INSERT OR UPDATE OR DELETE ON notifications
FOR EACH ROW EXECUTE FUNCTION notify_change();
In Node.js the following code connects to the database and receives the trigger updates in real-time:
const http = require('http');
const { Client } = require('pg');
const pgClient = new Client({
// connection details go here
});
pgClient.connect();
pgClient.query('LISTEN table_update');
pgClient.on('notification', (msg) => {
const payload = JSON.parse(msg.payload);
console.log('Notification received');
console.log(payload);
});
The question I would like to pose is: Could the above Node.js code be turned into a Wappler extension/custom-module (I'm not sure what the correct terminology is)? The extension should use the database connection configured in Wappler and provide the received payload as a JSON object which can be used by subsequent actions in the API end-point workflow?
I don't have any experience in building Wappler extensions, and don't really have the time to learn it from scratch. So any pointers on how to do this would be very much appreciated.