Server-Side Events / EventSource / PostgreSQL Notify - only when transaction in DB

824 Views Asked by At

I am trying to let client(browser) know when there is a transaction in database.
I am using EventSource in JS to connect with PHP where I 'listen' to PostgreSQL DB.
What is happening is that the EventSource listens or reads data every 3-4 seconds even though I have not set anything about it.
My intention is to get notified only when there is a transaction in DB and only when DB shoots 'notify'.

PROBLEM: The EventSource is listening every 3-4 seconds like polling and it notifies me multiple times even though there was one notification from DB.
CONCERN: I am connecting DB every time it runs through PHP and this SSE feels like polling.

JavaScript

if(typeof(EventSource) !== "undefined") {
    var source = new EventSource("sse.php");
    source.onmessage = function(event) {
        console.log(event.data);
    };
} else {
    console.log("SSE NOT SUPPORTED");
}

PHP

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$conn = pg_pconnect("DB INFO HERE") or die('DB CONNECT ERROR');
pg_query($conn, 'LISTEN event_insert;');
$notify = pg_get_notify($conn);

if (!$notify) {
    echo "data: .\n\n";
} else {
    $msg = json_encode($notify);
    echo "data: {$msg}\n\n";
}

flush();

PostgreSQL

CREATE OR REPLACE FUNCTION notify_event_insert() RETURNS trigger AS $$
DECLARE
BEGIN
  NOTIFY event_insert;
  RETURN new;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_event_insert AFTER INSERT ON events
FOR EACH ROW EXECUTE PROCEDURE notify_event_insert();

JS CONSOLE

I get '.' when there is no transaction, and I get JSON when there is transaction and the DB 'notifies' me.

Screenshot

1

There are 1 best solutions below

3
On

Polling is what it is documented to do:

Otherwise if no NOTIFY is waiting, then FALSE is returned.

Maybe you want to use PDO's pgsqlGetNotify instead.