Notify

NAME

Pg::Notify - Reactive interface to PostgreSQL notifications

SYNOPSIS


use Pg::Notify;
use DBIish;

my $db = DBIish.connect('Pg', database => "dbdishtest");
my $channel = "test";

my $notify = Pg::Notify.new(:$db, :$channel );

react {
    whenever $notify -> $notification {
        say $notification.extra;
    }
    whenever Supply.interval(1) -> $v {
        say $db.do("NOTIFY $channel, '$v'");
    }
}

DESCRIPTION

This provides a simple mechanism to get a supply of the PostgresQL notifications for a single or group of channels. The supply will emit a stream of pg-notify objects corresponding to a NOTIFY executed on the connected Postgres database.

Typically the NOTIFY will be invoked in a trigger or other server side code but could just as easily be in some other user code (as in the Synopsis above,)

The objects of type Pg::Notify have a Supply method that allows coercion in places that expect a Supply (such as whenever in the Synopsis above.) but you can this Supply directly if you want to tap it for instance.

METHODS

method new

method new(DBDish::Pg::Connection :$db, Str :@channel)

Both parameters are required. $db should be the result of a DBIish.connect to a Postgres database, @channel should be a list of NOTIFY topics that you want to receive.

Using multiple Pg::Notify on the same DB connection may result in lost notifications.

method Supply

method Supply() returns Supply

This returns the Supply onto which the notifications are emitted as they arrive, it will act as a coercer in certain places such as a whenever which expect a Supply.

The objects emitted are DBDish::Pg::Native::pg-notify with the following members:

relname

The name of the "topic" or "channel" the notification is for.

be_pid

The Process ID of the process in which the NOTIFY was executed.

extra

The string "payload" of the notification, it is optional on the NOTIFY command so may be undefined. There is a limit of about 8192 Bytes on the size of the oayload. The encoding of the payload is a function of the server configuration.

method unlisten()

method unlisten()

This will call UNLISTEN on the database connection and terminate the thread that polls for new notifications, after calling this if you want to resume listening for notifications you will need to create a new object.

Pg::Notify v0.0.7

Reactive interface to PostgreSQL notifies

Authors

  • Jonathan Stowe

License

Artistic-2.0

Dependencies

DBIishNativeHelpers::CStruct

Test Dependencies

Provides

  • Pg::Notify

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.