pg_get_notify

(PHP 4 >= 4.3.0, PHP 5)

pg_get_notifyPing database connection

Description

array pg_get_notify ( resource $connection [, int $result_type ] )

pg_get_notify() gets notify message sent by NOTIFY SQL command. To recieve nofigy messages, LISTEN SQL command must be issued. If there is notify message on the connection, array contains message name and backend PID is returned. If there is no message, FALSE is returned.

See also pg_get_pid()

Example #1 PostgreSQL NOTIFY message

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

// Listen 'author_updated' message from other processes
pq_query($conn'LISTEN author_updated;');
$notify pg_get_notify($conn);
if (!
$notify)
    print(
"No messages\n");
else
    
print_r($notify);
?>

add a note add a note

User Contributed Notes 1 note

up
0
glococo at gmail dot com
3 months ago
Instant yet Simple PHP notification with HTML5 Server-Sent Events

sse.php
<?php
$dbconn
= new PDO("pgsql:host=localhost;dbname=mydb", "pduser", "userpass");
$dbconn->exec('LISTEN "channel_name"');   // those doublequotes are very important

header("X-Accel-Buffering: no"); // disable ngnix webServer buffering
header("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->pgsqlGetNotify(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 Mozilla web site (ssedemo.php)
To Top