Mercury::Pattern::PubSub - Manage a pub/sub pattern for a single topic
version 0.016
# 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!' );
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.
Arrayref of connected websockets ready to receive messages
Arrayref of connected websockets ready to publish messages
$pat->add_subscriber( $tx );
Add the connection as a subscriber. Subscribers will receive all messages sent by publishers.
$pat->remove_subscriber( $tx );
Remove a subscriber. Called automatically when a subscriber socket is closed.
$pat->add_publisher( $tx );
Add a publisher to this topic. Publishers send messages to all subscribers.
$pat->remove_publisher( $tx );
Remove a publisher from the list. Called automatically when the publisher socket is closed.
$pat->send_message( $message );
Send a message to all subscribers.
Doug Bell <preaction@cpan.org>
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.
To install Mercury, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mercury
CPAN shell
perl -MCPAN -e shell install Mercury
For more information on module installation, please visit the detailed CPAN module installation guide.