1Mojo::Pg::PubSub(3)   User Contributed Perl Documentation  Mojo::Pg::PubSub(3)
2
3
4

NAME

6       Mojo::Pg::PubSub - Publish/Subscribe
7

SYNOPSIS

9         use Mojo::Pg::PubSub;
10
11         my $pubsub = Mojo::Pg::PubSub->new(pg => $pg);
12         my $cb = $pubsub->listen(foo => sub {
13           my ($pubsub, $payload) = @_;
14           say "Received: $payload";
15         });
16         $pubsub->notify(foo => 'I ♥ Mojolicious!');
17         $pubsub->unlisten(foo => $cb);
18

DESCRIPTION

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

EVENTS

26       Mojo::Pg::PubSub inherits all events from Mojo::EventEmitter and can
27       emit the following new ones.
28
29   disconnect
30         $pubsub->on(disconnect => sub {
31           my ($pubsub, $db) = @_;
32           ...
33         });
34
35       Emitted after the current database connection is lost.
36
37   reconnect
38         $pubsub->on(reconnect => sub {
39           my ($pubsub, $db) = @_;
40           ...
41         });
42
43       Emitted after switching to a new database connection for sending and
44       receiving notifications.
45

ATTRIBUTES

47       Mojo::Pg::PubSub implements the following attributes.
48
49   pg
50         my $pg  = $pubsub->pg;
51         $pubsub = $pubsub->pg(Mojo::Pg->new);
52
53       Mojo::Pg object this publish/subscribe container belongs to. Note that
54       this attribute is weakened.
55
56   reconnect_interval
57         my $interval = $pubsub->reconnect_interval;
58         $pubsub      = $pubsub->reconnect_interval(0.1);
59
60       Amount of time in seconds to wait to reconnect after disconnecting,
61       defaults to 1.
62

METHODS

64       Mojo::Pg::PubSub inherits all methods from Mojo::EventEmitter and
65       implements the following new ones.
66
67   db
68         my $db = $pubsub->db;
69
70       Build and cache or get cached Mojo::Pg::Database connection from "pg".
71       Used to reconnect if disconnected.
72
73         # Reconnect immediately
74         $pubsub->unsubscribe('disconnect')->on(disconnect => sub { shift->db });
75
76   json
77         $pubsub = $pubsub->json('foo');
78
79       Activate automatic JSON encoding and decoding with "to_json" in
80       Mojo::JSON and "from_json" in Mojo::JSON for a channel.
81
82         # Send and receive data structures
83         $pubsub->json('foo')->listen(foo => sub {
84           my ($pubsub, $payload) = @_;
85           say $payload->{bar};
86         });
87         $pubsub->notify(foo => {bar => 'I ♥ Mojolicious!'});
88
89   listen
90         my $cb = $pubsub->listen(foo => sub {...});
91
92       Subscribe to a channel, there is no limit on how many subscribers a
93       channel can have. Automatic decoding of JSON text to Perl values can be
94       activated with "json".
95
96         # Subscribe to the same channel twice
97         $pubsub->listen(foo => sub {
98           my ($pubsub, $payload) = @_;
99           say "One: $payload";
100         });
101         $pubsub->listen(foo => sub {
102           my ($pubsub, $payload) = @_;
103           say "Two: $payload";
104         });
105
106   new
107         my $pubsub = Mojo::Pg::PubSub->new;
108         my $pubsub = Mojo::Pg::PubSub->new(pg => Mojo::Pg->new);
109         my $pubsub = Mojo::Pg::PubSub->new({pg => Mojo::Pg->new});
110
111       Construct a new Mojo::Pg::PubSub object and subscribe to the
112       "disconnect" event with default reconnect logic.
113
114   notify
115         $pubsub = $pubsub->notify('foo');
116         $pubsub = $pubsub->notify(foo => 'I ♥ Mojolicious!');
117         $pubsub = $pubsub->notify(foo => {bar => 'baz'});
118
119       Notify a channel. Automatic encoding of Perl values to JSON text can be
120       activated with "json".
121
122   reset
123         $pubsub->reset;
124
125       Reset all subscriptions and the database connection. This is usually
126       done after a new process has been forked, to prevent the child process
127       from stealing notifications meant for the parent process.
128
129   unlisten
130         $pubsub = $pubsub->unlisten('foo');
131         $pubsub = $pubsub->unlisten(foo => $cb);
132
133       Unsubscribe from a channel.
134

SEE ALSO

136       Mojo::Pg, Mojolicious::Guides, <https://mojolicious.org>.
137
138
139
140perl v5.30.1                      2020-02-02               Mojo::Pg::PubSub(3)
Impressum