1Mojo::Pg::PubSub(3) User Contributed Perl Documentation Mojo::Pg::PubSub(3)
2
3
4
6 Mojo::Pg::PubSub - Publish/Subscribe
7
9 use Mojo::Pg::PubSub;
10
11 my $pubsub = Mojo::Pg::PubSub->new(pg => $pg);
12 my $cb = $pubsub->listen(foo => sub ($pubsub, $payload) {
13 say "Received: $payload";
14 });
15 $pubsub->notify(foo => 'I ♥ Mojolicious!');
16 $pubsub->unlisten(foo => $cb);
17
19 Mojo::Pg::PubSub is a scalable implementation of the publish/subscribe
20 pattern used by Mojo::Pg. It is based on PostgreSQL notifications and
21 allows many consumers to share the same database connection, to avoid
22 many common scalability problems.
23
25 Mojo::Pg::PubSub inherits all events from Mojo::EventEmitter and can
26 emit the following new ones.
27
28 disconnect
29 $pubsub->on(disconnect => sub ($pubsub, $db) {
30 ...
31 });
32
33 Emitted after the current database connection is lost.
34
35 reconnect
36 $pubsub->on(reconnect => sub ($pubsub, $db) {
37 ...
38 });
39
40 Emitted after switching to a new database connection for sending and
41 receiving notifications.
42
44 Mojo::Pg::PubSub implements the following attributes.
45
46 pg
47 my $pg = $pubsub->pg;
48 $pubsub = $pubsub->pg(Mojo::Pg->new);
49
50 Mojo::Pg object this publish/subscribe container belongs to. Note that
51 this attribute is weakened.
52
53 reconnect_interval
54 my $interval = $pubsub->reconnect_interval;
55 $pubsub = $pubsub->reconnect_interval(0.1);
56
57 Amount of time in seconds to wait to reconnect after disconnecting,
58 defaults to 1.
59
61 Mojo::Pg::PubSub inherits all methods from Mojo::EventEmitter and
62 implements the following new ones.
63
64 db
65 my $db = $pubsub->db;
66
67 Build and cache or get cached Mojo::Pg::Database connection from "pg".
68 Used to reconnect if disconnected.
69
70 # Reconnect immediately
71 $pubsub->unsubscribe('disconnect')->on(disconnect => sub ($pubsub, $db) { pubsub->db });
72
73 json
74 $pubsub = $pubsub->json('foo');
75
76 Activate automatic JSON encoding and decoding with "to_json" in
77 Mojo::JSON and "from_json" in Mojo::JSON for a channel.
78
79 # Send and receive data structures
80 $pubsub->json('foo')->listen(foo => sub ($pubsub, $payload) {
81 say $payload->{bar};
82 });
83 $pubsub->notify(foo => {bar => 'I ♥ Mojolicious!'});
84
85 listen
86 my $cb = $pubsub->listen(foo => sub {...});
87
88 Subscribe to a channel, there is no limit on how many subscribers a
89 channel can have. Automatic decoding of JSON text to Perl values can be
90 activated with "json".
91
92 # Subscribe to the same channel twice
93 $pubsub->listen(foo => sub ($pubsub, $payload) {
94 say "One: $payload";
95 });
96 $pubsub->listen(foo => sub ($pubsub, $payload) {
97 say "Two: $payload";
98 });
99
100 new
101 my $pubsub = Mojo::Pg::PubSub->new;
102 my $pubsub = Mojo::Pg::PubSub->new(pg => Mojo::Pg->new);
103 my $pubsub = Mojo::Pg::PubSub->new({pg => Mojo::Pg->new});
104
105 Construct a new Mojo::Pg::PubSub object and subscribe to the
106 "disconnect" event with default reconnect logic.
107
108 notify
109 $pubsub = $pubsub->notify('foo');
110 $pubsub = $pubsub->notify(foo => 'I ♥ Mojolicious!');
111 $pubsub = $pubsub->notify(foo => {bar => 'baz'});
112
113 Notify a channel. Automatic encoding of Perl values to JSON text can be
114 activated with "json".
115
116 reset
117 $pubsub->reset;
118
119 Reset all subscriptions and the database connection. This is usually
120 done after a new process has been forked, to prevent the child process
121 from stealing notifications meant for the parent process.
122
123 unlisten
124 $pubsub = $pubsub->unlisten('foo');
125 $pubsub = $pubsub->unlisten(foo => $cb);
126
127 Unsubscribe from a channel.
128
130 Mojo::Pg, Mojolicious::Guides, <https://mojolicious.org>.
131
132
133
134perl v5.36.0 2023-01-20 Mojo::Pg::PubSub(3)