Re: Is there any way to listen to NOTIFY in php without polling? - Mailing list pgsql-general

From Daniel Verite
Subject Re: Is there any way to listen to NOTIFY in php without polling?
Date
Msg-id 05e98187-8cda-4e80-9ece-f4c14247d321@mm
Whole thread Raw
In response to Re: Is there any way to listen to NOTIFY in php without polling?  (Misa Simic <misa.simic@gmail.com>)
List 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


pgsql-general by date:

Previous
From: "Ed L."
Date:
Subject: unique indices without pg_constraint rows
Next
From: Tom Lane
Date:
Subject: Re: unique indices without pg_constraint rows