NAME
Mercury::Pattern::PubSub - Manage a pub/sub pattern for a single topic
VERSION
version 0.016
SYNOPSIS
# Connect the publisher
my $pub_ua = Mojo::UserAgent->new;
my $pub_tx = $ua->websocket( '/pub/foo' );
# Connect the subscriber socket
my $sub_ua = Mojo::UserAgent->new;
my $sub_tx = $ua->websocket( '/sub/foo' );
# Connect the two sockets using pub/sub
my $pattern = Mercury::Pattern::PubSub->new;
$pattern->add_publisher( $pub_tx );
$pattern->add_subscriber( $sub_tx );
# Send a message
$sub_tx->on( message => sub {
my ( $tx, $msg ) = @_;
print $msg; # Hello, World!
} );
$pub_tx->send( 'Hello, World!' );
DESCRIPTION
This pattern connects publishers, which send messages, to subscribers, which recieve messages. Each message sent by a publisher will be received by all connected subscribers. This pattern is useful for sending notification events and logging.
ATTRIBUTES
subscribers
Arrayref of connected websockets ready to receive messages
publishers
Arrayref of connected websockets ready to publish messages
METHODS
add_subscriber
$pat->add_subscriber( $tx );
Add the connection as a subscriber. Subscribers will receive all messages sent by publishers.
remove_subscriber
$pat->remove_subscriber( $tx );
Remove a subscriber. Called automatically when a subscriber socket is closed.
add_publisher
$pat->add_publisher( $tx );
Add a publisher to this topic. Publishers send messages to all subscribers.
remove_publisher
$pat->remove_publisher( $tx );
Remove a publisher from the list. Called automatically when the publisher socket is closed.
send_message
$pat->send_message( $message );
Send a message to all subscribers.
SEE ALSO
AUTHOR
Doug Bell <preaction@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2018 by Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.