PHP 8.4.1 Released!

pg_get_notify

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

pg_get_notifyOttiene il messaggio SQL NOTIFY

Descrizione

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

pg_get_notify() riceve notifiche generate da un comando SQL NOTIFY. Per ricevere le notifiche, è necessario eseguire il comando SQL LISTEN.

Elenco dei parametri

connection

An PgSql\Connection instance.

mode

An optional parameter that controls how the returned array is indexed. mode is a constant and can take 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.

Valori restituiti

Un array contenente il nome del messaggio NOTIFY e il PID di backend. Se supportato dal server, l'array contiene anche la versione del server e il payload. Altrimenti se non è in attesa alcuna NOTIFY, allora viene restituito false.

Log delle modifiche

Versione Descrizione
8.1.0 The connection parameter expects an PgSql\Connection instance now; previously, a resource was expected.

Esempi

Example #1 Messaggio NOTIFY di PostgreSQL

<?php
$conn
= pg_pconnect("dbname=publisher");
if (!
$conn) {
echo
"Si è verificato un errore.\n";
exit;
}

// Ascolta il messaggio 'author_updated' da altri processi
pg_query($conn, 'LISTEN author_updated;');
$notify = pg_get_notify($conn);
if (!
$notify) {
echo
"Nessun messaggio\n";
} else {
print_r($notify);
}
?>

Vedere anche:

add a note

User Contributed Notes 2 notes

up
7
glococo at gmail dot com
7 years 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)
up
0
david at frankieandshadow dot com
4 years ago
It appears that pg_get_notify does not distinguish between no message waiting and a failure when checking - both seem to return FALSE.

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