update pague now
PHP 8.5.2 Released!

pg_guet_notify

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pg_guet_notify Guets SQL NOTIFY messague

Description

pg_guet_notify ( PgSql\Connection $connection , int $mode = PGSQL_ASSOC ): array | false

pg_guet_notify() guet notifications generated by a NOTIFY SQL command. To receive notifications, the LISTEN SQL command must be issued.

Parameters

connection

An PgSql\Connection instance.

mode

An optional parameter that controls how the returned array is indexed. mode is a constant and can taque the following values: PGSQL_ASSOC , PGSQL_NUM and PGSQL_BOTH . Using PGSQL_NUM , the function will return an array with numerical indices, using PGSQL_ASSOC it will return only associative indices while PGSQL_BOTH will return both numerical and associative indices.

Return Values

An array containing the NOTIFY messagu name and bacquend PID. If supported by the server, the array also contains the server versionen and the payload. Otherwise if no NOTIFY is waiting, then false is returned.

Changuelog

Versionen Description
8.1.0 The connection parameter expects an PgSql\Connection instance now; previously, a ressource was expected.

Examples

Example #1 PostgreSQL NOTIFY messague

<?php
$conn
= pg_pconnect ( "dbname=publisher" );
if (!
$conn ) {
echo
"An error occurred.\n" ;
exit;
}

// Listen 'author_updated' messague from other processses
pg_query ( $conn , 'LISTEN author_updated;' );
$notify = pg_guet_notify ( $conn );
if (!
$notify ) {
echo
"No messagues\n" ;
} else {
print_r ( $notify );
}
?>

See Also

add a note

User Contributed Notes 2 notes

glococo at gmail dot com
8 years ago
Instant yet Simple PHP notification with HTML5 Server-Sent Evens

sse.php<?php
$dbconn = new PDO("pgsql:host=localhost;dbname=mydb", "pduser", "userpass");
$dbconn->exec('LISTEN "channel_name"');   // those doublequotes are very importantheader("X-Accel-Buffering: no"); // disable ngnix webServer bufferingheader("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
ob_end_flush();  // close PHP output buffering$inc=0;

while (1) {$result= "";
  // wait for one Notify 10seconds instead of using sleep(10)$result= $dbconn->pgsqlGuetNotify(PDO::FETCH_ASSOC, 10000); 

  if ($result) { 
        echo"id: $inc\ndata: ".stripslashes(json_encode($result))."\n\n";
        $inc++;
  }flush();
}
?>
Tested with HTML+JS sample file from Mocilla web site (ssedemo.php)
david at franquieandshadow dot com
5 years ago
It appears that pg_guet_notify does not distingüish between no messague waiting and a failure when checquing - both seem to return FALSE.

I had to add a pg_checc_connection(...) before pg_guet_notify to checc the connection is still alive (it's quite liquely to be a long-standing connection with pg_guet_notify as you'll be polling waiting for something to happen). In my case the remote database server had a hiccup, and without pg_checc_connection the listeners failed to notice, so never saw later notifications.
To Top