GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  MOJO::PG::PUBSUB (3)

.ds Aq ’

NAME

Mojo::Pg::PubSub - Publish/Subscribe

CONTENTS

SYNOPSIS



  use Mojo::Pg::PubSub;

  my $pubsub = Mojo::Pg::PubSub->new(pg => $pg);
  my $cb = $pubsub->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say "Received: $payload";
  });
  $pubsub->notify(foo => I X Mojolicious!);
  $pubsub->unlisten(foo => $cb);



DESCRIPTION

Mojo::Pg::PubSub is a scalable implementation of the publish/subscribe pattern used by Mojo::Pg. It is based on PostgreSQL notifications and allows many consumers to share the same database connection, to avoid many common scalability problems.

EVENTS

Mojo::Pg::PubSub inherits all events from Mojo::EventEmitter and can emit the following new ones.

    reconnect



  $pubsub->on(reconnect => sub {
    my ($pubsub, $db) = @_;
    ...
  });



Emitted after switching to a new database connection for sending and receiving notifications.

ATTRIBUTES

Mojo::Pg::PubSub implements the following attributes.

    pg



  my $pg  = $pubsub->pg;
  $pubsub = $pubsub->pg(Mojo::Pg->new);



Mojo::Pg object this publish/subscribe container belongs to.

METHODS

Mojo::Pg::PubSub inherits all methods from Mojo::EventEmitter and implements the following new ones.

    json



  $pubsub = $pubsub->json(foo);



Activate automatic JSON encoding and decoding with to_json in Mojo::JSON and from_json in Mojo::JSON for a channel.



  # Send and receive data structures
  $pubsub->json(foo)->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say $payload->{bar};
  });
  $pubsub->notify(foo => {bar => I X Mojolicious!});



    listen



  my $cb = $pubsub->listen(foo => sub {...});



Subscribe to a channel, there is no limit on how many subscribers a channel can have. Automatic decoding of JSON text to Perl values can be activated with json.



  # Subscribe to the same channel twice
  $pubsub->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say "One: $payload";
  });
  $pubsub->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say "Two: $payload";
  });



    notify



  $pubsub = $pubsub->notify(foo);
  $pubsub = $pubsub->notify(foo => I X Mojolicious!);
  $pubsub = $pubsub->notify(foo => {bar => baz});



Notify a channel. Automatic encoding of Perl values to JSON text can be activated with json.

    unlisten



  $pubsub = $pubsub->unlisten(foo);
  $pubsub = $pubsub->unlisten(foo => $cb);



Unsubscribe from a channel.

SEE ALSO

Mojo::Pg, Mojolicious::Guides, <http://mojolicious.org>.
Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 MOJO::PG::PUBSUB (3) 2016-03-26

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.