<?php
//===========notifier.sql:
/*
CREATE OR REPLACE FUNCTION public.notify_channel()
RETURNS trigger
AS $function$
BEGIN
PERFORM pg_notify('channel_name', row_to_json(NEW)::text);
RETURN NULL;
END;
$function$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_on_insert AFTER INSERT ON mytable
FOR EACH ROW EXECUTE PROCEDURE notify_channel();
*/
set_time_limit(0);
$db = new PDO(
'pgsql:dbname=dbname host=host port=5432;options=--application_name=APPLICATION_NAME',
'user',
'password',
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
$db->exec('LISTEN channel_name');
while (true) {
while ($db->pgsqlGetNotify(PDO::FETCH_ASSOC, 30000)) {
echo json_encode($result).PHP_EOL;
}
}
Event Notify Listener made with PHP.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.