http://www.perlmonks.org?node_id=1082121

xtpu2 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to make use of the Postgres Listen/Notify feature in my AnyEvent-based program. I tried looking for a ready solution and found AnyEvent::Pg on CPAN, but I've not been able to get the module to work (and the developer does mention that it is in very early development). So I was trying to figure out the best way to set this up with the tools I have.

Listen/Notify is supported by DBD:Pg, so the solution I came up with is to poll $dbh to see if a notification has been received, like so (posting only the relevant code):

my $w = AnyEvent->timer ( after => 0, interval => 2, cb => sub { if (my $notify = $dbh->pg_notifies){ # Do something } } );

This works. However, I'm not sure this is the best way to do it because I'm just not that familiar with event-based programming. I would really appreciate alternate solutions / thoughts from more knowledgeable people...

Replies are listed 'Best First'.
Re: AnyEvent and Postgres Listen/Notify
by salva (Canon) on Apr 14, 2014 at 07:06 UTC
    Hi, I am the author of AnyEvent::Pg.

    I could help you to get the module running if you describe your issues. The module is still pretty young and in most cases DBI/DBD::Pg is the preferred choice, but I use the LISTEN/NOTIFY feature myself and it works well, at least on Linux systems.

Re: AnyEvent and Postgres Listen/Notify
by nikosv (Deacon) on Apr 14, 2014 at 06:57 UTC
    You could use the inherent DBMS's database events. You make an application, preferably set up as a service for constant monitoring, that registers and listens for the event. As soon as the event is raised the service gets the event details (name,text) and acts upon it.

    For a good example on it check the event.t source of DBD-IngresII. Might be Ingres specific and not Postgres but it illustrates the point clearly