Re: Is there any way to listen to NOTIFY in php without polling?

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Is there any way to listen to NOTIFY in php without polling?
Date: 2013-03-29 23:13:11
Message-ID: 05e98187-8cda-4e80-9ece-f4c14247d321@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Misa Simic wrote:

> I am not a C developer - was not aware about select()... I was read it as
> some kind of sleep...

php provides socket_select() as an equivalent to C's select().
See http://php.net/manual/en/function.socket-select.php

But it takes "socket resources" as arguments and the postgres php layer
does not provides a conversion from the file descriptor returned by
PQsocket() to such php socket resources.
Thus socket_select() can't be used to monitor a postgres connection.

However, you can get a behavior that's close enough in practice to
select() with code like this:

pg_query($conn, "LISTEN event_name");
while (!$end) {
$arr=pg_get_notify($conn);
if (!$arr) {
usleep(100000);
}
else
// process the notification
}

This will catch a notification 1/10 second max after it's available and sleep
the rest of the time.

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2013-03-29 23:30:52 Re: unique indices without pg_constraint rows
Previous Message Ed L. 2013-03-29 23:04:42 unique indices without pg_constraint rows