[PHP-NOTES] note 130608 added to pdo-pgsql.getnotify

[email protected] ("[email protected]") Sun, 11 Jan 2026 23:33:47 +0000
Newsgroups php.notes
Message-ID <[email protected]>
This page needs an example to understand that you **need** to explicitly call LISTEN before using getNotify, like shown in https://www.php.net/manual/en/function.pg-get-notify.php

<?php

$db = new PDO($dsn, $user, $password, $options);
$db->query('LISTEN test');
$notification = $db->pgsqlGetNotify(PDO::FETCH_ASSOC, 10000);

// or

$db = new Pdo\Pgsql($dsn, $user, $password, $options);
$db->query('LISTEN test');
$notification = $db->getNotify(PDO::FETCH_ASSOC, 10000);

// now you can call NOTIFY elsewhere
// PG> NOTIFY test, 'payload string';
var_dump($notification);

?>

array(3) {
  ["message"]=>
  string(4) "test"
  ["pid"]=>
  int(123565)
  ["payload"]=>
  string(14) "payload string"
}

If you called NOTIFY before calling LISTEN, nothing will be returned!

You receive the first notification only, and you have to call getNotify again. And call LISTEN again if DB connection drops.
----
Server IP: 206.189.2.9
Probable Submitter: 2a01:c846:3901:e400:35aa:d235:8839:f46f
----
Manual Page -- https://php.net/manual/en/pdo-pgsql.getnotify.php
Edit        -- https://main.php.net/note/edit/130608
Del: integrated  -- https://main.php.net/note/delete/130608/integrated
Del: useless     -- https://main.php.net/note/delete/130608/useless
Del: bad code    -- https://main.php.net/note/delete/130608/bad+code
Del: spam        -- https://main.php.net/note/delete/130608/spam
Del: non-english -- https://main.php.net/note/delete/130608/non-english
Del: in docs     -- https://main.php.net/note/delete/130608/in+docs
Del: other reasons-- https://main.php.net/note/delete/130608
Reject      -- https://main.php.net/note/reject/130608
Search      -- https://main.php.net/manage/user-notes.php