.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::Pg::PubSub 3pm" .TH Mojo::Pg::PubSub 3pm "2022-03-19" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::Pg::PubSub \- Publish/Subscribe .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::Pg::PubSub; \& \& my $pubsub = Mojo::Pg::PubSub\->new(pg => $pg); \& my $cb = $pubsub\->listen(foo => sub ($pubsub, $payload) { \& say "Received: $payload"; \& }); \& $pubsub\->notify(foo => \*(AqI ♥ Mojolicious!\*(Aq); \& $pubsub\->unlisten(foo => $cb); .Ve .SH "DESCRIPTION" .IX Header "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. .SH "EVENTS" .IX Header "EVENTS" Mojo::Pg::PubSub inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS "disconnect" .IX Subsection "disconnect" .Vb 3 \& $pubsub\->on(disconnect => sub ($pubsub, $db) { \& ... \& }); .Ve .PP Emitted after the current database connection is lost. .SS "reconnect" .IX Subsection "reconnect" .Vb 3 \& $pubsub\->on(reconnect => sub ($pubsub, $db) { \& ... \& }); .Ve .PP Emitted after switching to a new database connection for sending and receiving notifications. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mojo::Pg::PubSub implements the following attributes. .SS "pg" .IX Subsection "pg" .Vb 2 \& my $pg = $pubsub\->pg; \& $pubsub = $pubsub\->pg(Mojo::Pg\->new); .Ve .PP Mojo::Pg object this publish/subscribe container belongs to. Note that this attribute is weakened. .SS "reconnect_interval" .IX Subsection "reconnect_interval" .Vb 2 \& my $interval = $pubsub\->reconnect_interval; \& $pubsub = $pubsub\->reconnect_interval(0.1); .Ve .PP Amount of time in seconds to wait to reconnect after disconnecting, defaults to \f(CW1\fR. .SH "METHODS" .IX Header "METHODS" Mojo::Pg::PubSub inherits all methods from Mojo::EventEmitter and implements the following new ones. .SS "db" .IX Subsection "db" .Vb 1 \& my $db = $pubsub\->db; .Ve .PP Build and cache or get cached Mojo::Pg::Database connection from \*(L"pg\*(R". Used to reconnect if disconnected. .PP .Vb 2 \& # Reconnect immediately \& $pubsub\->unsubscribe(\*(Aqdisconnect\*(Aq)\->on(disconnect => sub ($pubsub, $db) { pubsub\->db }); .Ve .SS "json" .IX Subsection "json" .Vb 1 \& $pubsub = $pubsub\->json(\*(Aqfoo\*(Aq); .Ve .PP Activate automatic \s-1JSON\s0 encoding and decoding with \*(L"to_json\*(R" in Mojo::JSON and \*(L"from_json\*(R" in Mojo::JSON for a channel. .PP .Vb 5 \& # Send and receive data structures \& $pubsub\->json(\*(Aqfoo\*(Aq)\->listen(foo => sub ($pubsub, $payload) { \& say $payload\->{bar}; \& }); \& $pubsub\->notify(foo => {bar => \*(AqI ♥ Mojolicious!\*(Aq}); .Ve .SS "listen" .IX Subsection "listen" .Vb 1 \& my $cb = $pubsub\->listen(foo => sub {...}); .Ve .PP Subscribe to a channel, there is no limit on how many subscribers a channel can have. Automatic decoding of \s-1JSON\s0 text to Perl values can be activated with \*(L"json\*(R". .PP .Vb 7 \& # Subscribe to the same channel twice \& $pubsub\->listen(foo => sub ($pubsub, $payload) { \& say "One: $payload"; \& }); \& $pubsub\->listen(foo => sub ($pubsub, $payload) { \& say "Two: $payload"; \& }); .Ve .SS "new" .IX Subsection "new" .Vb 3 \& my $pubsub = Mojo::Pg::PubSub\->new; \& my $pubsub = Mojo::Pg::PubSub\->new(pg => Mojo::Pg\->new); \& my $pubsub = Mojo::Pg::PubSub\->new({pg => Mojo::Pg\->new}); .Ve .PP Construct a new Mojo::Pg::PubSub object and subscribe to the \*(L"disconnect\*(R" event with default reconnect logic. .SS "notify" .IX Subsection "notify" .Vb 3 \& $pubsub = $pubsub\->notify(\*(Aqfoo\*(Aq); \& $pubsub = $pubsub\->notify(foo => \*(AqI ♥ Mojolicious!\*(Aq); \& $pubsub = $pubsub\->notify(foo => {bar => \*(Aqbaz\*(Aq}); .Ve .PP Notify a channel. Automatic encoding of Perl values to \s-1JSON\s0 text can be activated with \*(L"json\*(R". .SS "reset" .IX Subsection "reset" .Vb 1 \& $pubsub\->reset; .Ve .PP Reset all subscriptions and the database connection. This is usually done after a new process has been forked, to prevent the child process from stealing notifications meant for the parent process. .SS "unlisten" .IX Subsection "unlisten" .Vb 2 \& $pubsub = $pubsub\->unlisten(\*(Aqfoo\*(Aq); \& $pubsub = $pubsub\->unlisten(foo => $cb); .Ve .PP Unsubscribe from a channel. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojo::Pg, Mojolicious::Guides, .