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

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

From
Clemens Eisserer
Date:
Hi,

Sorry for asking such a newbie-question, I've used a search engine -
however I haven't found what I am searching for.

Is there any way to listen to NOTIFY in php without polling using a
callback or blocking call?
I've only found pg_get_notify(), however it requires polling as far as
I understand.

Thank you in advance, Clemens


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

From
Bill Moran
Date:
In response to Clemens Eisserer <linuxhippy@gmail.com>:
>
> Is there any way to listen to NOTIFY in php without polling using a
> callback or blocking call?

Not at this time.

--
Bill Moran
http://www.potentialtech.com
http://people.collaborativefusion.com/~wmoran/


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

From
Clemens Eisserer
Date:
Hi Bill,

>> Is there any way to listen to NOTIFY in php without polling using a
>> callback or blocking call?
>
> Not at this time.

Too bad ... Thanks for the confirmation.

I'll try to invoke a native libpg binary which stays alive until a
NOTIFY is received, should do the trick in case update-frequency is
low.

Thanks, Clemens


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

From
Misa Simic
Date:
Hi,

What is the main goal?

even using libpg - you need to call pg notify... Doc says, just using libpgtcl would be possible to get Notify event - without checking from time to time...

Kind Regards,

Misa



2013/3/27 Clemens Eisserer <linuxhippy@gmail.com>
Hi Bill,

>> Is there any way to listen to NOTIFY in php without polling using a
>> callback or blocking call?
>
> Not at this time.

Too bad ... Thanks for the confirmation.

I'll try to invoke a native libpg binary which stays alive until a
NOTIFY is received, should do the trick in case update-frequency is
low.

Thanks, Clemens


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

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

From
Clemens Eisserer
Date:
Hi Misa

> What is the main goal?

The main goal is to perform some inter-system communication in the
case some rows in one table are updated (very seldom event).

> even using libpg - you need to call pg notify... Doc says, just using
> libpgtcl would be possible to get Notify event - without checking from time
> to time...

I found example 27-2 at
http://www.postgresql.org/docs/8.0/static/libpq-example.html , that
does exactly what I was looking for using low-level socket functions.
It seems to me this solution works without polling.

Regards, Clemens

PS: Compared to the libpq and php interfaces, the jdbc driver does a
really good job providing a useable interface for listen/notify to the
developers. Thanks :)


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

From
Misa Simic
Date:
Hi Clemens,

Well, I am not sure what you mean by polling...

But Example shows - that C app - actually asks all the time to get notify... when gets something more then 4 times - exit... until 4 times loops...

The same you can achieve with PHP...

But I am not sure that is the main goal...

My guess is that main goal is let Web App know - something happened in database...

I am not aware about something else in JDBC then the same principle...

One object (listner) - in background thread - always asks for notify DB - if gets something - fires event... (then other Java objects - if subscribed on Listner - do whatever need to doon that event....)

If we want - to let WebPage aware about what happened - still we are in the problem... (if we will not use - some kind of polling from WebPage)

Potential Solution: websocket

The same solution would be possible and with php...

But better to dont make this mail more complex - if that is not the goal... :)

Kind Regards,

Misa









2013/3/27 Clemens Eisserer <linuxhippy@gmail.com>
Hi Misa

> What is the main goal?

The main goal is to perform some inter-system communication in the
case some rows in one table are updated (very seldom event).

> even using libpg - you need to call pg notify... Doc says, just using
> libpgtcl would be possible to get Notify event - without checking from time
> to time...

I found example 27-2 at
http://www.postgresql.org/docs/8.0/static/libpq-example.html , that
does exactly what I was looking for using low-level socket functions.
It seems to me this solution works without polling.

Regards, Clemens

PS: Compared to the libpq and php interfaces, the jdbc driver does a
really good job providing a useable interface for listen/notify to the
developers. Thanks :)


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

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

From
Clemens Eisserer
Date:
Hi Misa

> But Example shows - that C app - actually asks all the time to get notify...
> when gets something more then 4 times - exit... until 4 times loops...
> The same you can achieve with PHP...

As far as I understood, with php I have to query the server again and
again, and pg_get_notify will either return something or not depending
on the query result:
http://php.net/manual/en/function.pg-get-notify.php

The provided sample however blocks until a notify is received (and
does so 4 times just for demonstration puporse), so instead of
frequently asking the server for notifications (polling), it wakes up
when a notification arrives.

> My guess is that main goal is let Web App know - something happened in
> database...
It isn't ;)

Regards, Clemens


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

From
Jasen Betts
Date:
On 2013-03-27, Misa Simic <misa.simic@gmail.com> wrote:
> --20cf3074d6a0c370ce04d8ef50c1
> Content-Type: text/plain; charset=UTF-8
>
> Hi Clemens,
>
> Well, I am not sure what you mean by polling...
>
> But Example shows - that C app - actually asks all the time to get
> notify... when gets something more then 4 times - exit... until 4 times
> loops...

that's polling.

I just do a select() on the socket and the select either times out or
lets me know something has come from the server. then I call whatever
it is to check for notifies.

> The same you can achieve with PHP...

except PHP doesn't have select() and hides the socket.

--
⚂⚃ 100% natural

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

From
Misa Simic
Date:
Thanks Janes...

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

Than Clemens explained to me what select() does...

However - to me it is just implementation detail... Which is possible in one language, but not in another...

But technically, is the loop inside select() - or outside is irrelevant to me (i see the benefit... Though, not sure the benefit - is that big...nowdays.....)

From my point of view - what is important - is the main goal what should be achieved...

If with resources what I have (php - though I am not php developer either) I dont have the same power like in another language - who cares - main goal must be achived . things work - or dont, but we must make them work :) 

I am just curious:

LISTEN foo
Select()

Another client says: NOTIFY bar

Will select() - get something from server?


Thanks,

Misa

On Thursday, March 28, 2013, Jasen Betts wrote:
On 2013-03-27, Misa Simic <misa.simic@gmail.com> wrote:
> --20cf3074d6a0c370ce04d8ef50c1
> Content-Type: text/plain; charset=UTF-8
>
> Hi Clemens,
>
> Well, I am not sure what you mean by polling...
>
> But Example shows - that C app - actually asks all the time to get
> notify... when gets something more then 4 times - exit... until 4 times
> loops...

that's polling.

I just do a select() on the socket and the select either times out or
lets me know something has come from the server. then I call whatever
it is to check for notifies.

> The same you can achieve with PHP...

except PHP doesn't have select() and hides the socket.

--
⚂⚃ 100% natural



--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

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

From
"Daniel Verite"
Date:
    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