pg_get_notify
(PHP 4 >= 4.3.0, PHP 5)
pg_get_notify — Gets SQL NOTIFY message
Description
$connection
[, int $result_type
] )pg_get_notify() gets notifications generated by a NOTIFY SQL command. To receive notifications, the LISTEN SQL command must be issued.
Parameters
-
connection
-
PostgreSQL database connection resource.
-
result_type
-
An optional parameter that controls how the returned array is indexed.
result_type
is a constant and can take the following values:PGSQL_ASSOC
,PGSQL_NUM
andPGSQL_BOTH
. UsingPGSQL_NUM
, pg_get_notify() will return an array with numerical indices, usingPGSQL_ASSOC
it will return only associative indices whilePGSQL_BOTH
, the default, will return both numerical and associative indices.
Return Values
An array containing the NOTIFY message name and backend PID.
Otherwise if no NOTIFY is waiting, then FALSE
is returned.
Examples
Example #1 PostgreSQL NOTIFY message
<?php
$conn = pg_pconnect("dbname=publisher");
if (!$conn) {
echo "An error occurred.\n";
exit;
}
// Listen 'author_updated' message from other processes
pg_query($conn, 'LISTEN author_updated;');
$notify = pg_get_notify($conn);
if (!$notify) {
echo "No messages\n";
} else {
print_r($notify);
}
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- PostgreSQL
- pg_affected_rows
- pg_cancel_query
- pg_client_encoding
- pg_close
- pg_connect_poll
- pg_connect
- pg_connection_busy
- pg_connection_reset
- pg_connection_status
- pg_consume_input
- pg_convert
- pg_copy_from
- pg_copy_to
- pg_dbname
- pg_delete
- pg_end_copy
- pg_escape_bytea
- pg_escape_identifier
- pg_escape_literal
- pg_escape_string
- pg_execute
- pg_fetch_all_columns
- pg_fetch_all
- pg_fetch_array
- pg_fetch_assoc
- pg_fetch_object
- pg_fetch_result
- pg_fetch_row
- pg_field_is_null
- pg_field_name
- pg_field_num
- pg_field_prtlen
- pg_field_size
- pg_field_table
- pg_field_type_oid
- pg_field_type
- pg_flush
- pg_free_result
- pg_get_notify
- pg_get_pid
- pg_get_result
- pg_host
- pg_insert
- pg_last_error
- pg_last_notice
- pg_last_oid
- pg_lo_close
- pg_lo_create
- pg_lo_export
- pg_lo_import
- pg_lo_open
- pg_lo_read_all
- pg_lo_read
- pg_lo_seek
- pg_lo_tell
- pg_lo_truncate
- pg_lo_unlink
- pg_lo_write
- pg_meta_data
- pg_num_fields
- pg_num_rows
- pg_options
- pg_parameter_status
- pg_pconnect
- pg_ping
- pg_port
- pg_prepare
- pg_put_line
- pg_query_params
- pg_query
- pg_result_error_field
- pg_result_error
- pg_result_seek
- pg_result_status
- pg_select
- pg_send_execute
- pg_send_prepare
- pg_send_query_params
- pg_send_query
- pg_set_client_encoding
- pg_set_error_verbosity
- pg_socket
- pg_trace
- pg_transaction_status
- pg_tty
- pg_unescape_bytea
- pg_untrace
- pg_update
- pg_version
Коментарии
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)
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.