| 1 |
NAME |
| 2 |
IPC::PubSub - Interprocess Publish/Subscribe channels |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
# A new message bus with the DBM::Deep backend |
| 6 |
# (Other possible backends include Memcached and PlainHash) |
| 7 |
my $bus = IPC::PubSub->new(DBM_Deep => '/tmp/pubsub.db'); |
| 8 |
|
| 9 |
# A channel is any arbitrary string |
| 10 |
my $channel = '#perl6'; |
| 11 |
|
| 12 |
# Register a new publisher (you can publish to multiple channels) |
| 13 |
my $pub = $bus->new_publisher("#perl6", "#moose"); |
| 14 |
|
| 15 |
# Publish a message (may be a complex object) to those channels |
| 16 |
$pub->msg("This is a message"); |
| 17 |
|
| 18 |
# Register a new subscriber (you can subscribe to multiple channels) |
| 19 |
my $sub = $bus->new_subscriber("#moose"); |
| 20 |
|
| 21 |
# Publish an object to channels |
| 22 |
$pub->msg("This is another message"); |
| 23 |
|
| 24 |
# Set all subsequent messages from this publisher to expire in 30 seconds |
| 25 |
$pub->expiry(30); |
| 26 |
$pub->msg("This message will go away in 30 seconds"); |
| 27 |
|
| 28 |
# Simple get: Returns the messages sent since the previous get, |
| 29 |
# but only for the first channel. |
| 30 |
my @msgs = $sub->get; |
| 31 |
|
| 32 |
# Simple get, with an explicit channel key (must be among the ones |
| 33 |
# it initially subscribed to) |
| 34 |
my @moose_msgs = $sub->get("#moose"); |
| 35 |
|
| 36 |
# Complex get: Returns a hash reference from channels to array |
| 37 |
# references of [timestamp, message]. |
| 38 |
my $hash_ref = $sub->get_all; |
| 39 |
|
| 40 |
# Changing the list of channels we subscribe to |
| 41 |
$sub->subscribe('some-other-channel'); |
| 42 |
$sub->unsubscribe('some-other-channel'); |
| 43 |
|
| 44 |
# Changing the list of channels we publish to |
| 45 |
$pub->publish('some-other-channel'); |
| 46 |
$pub->unpublish('some-other-channel'); |
| 47 |
|
| 48 |
# Listing and checking if we are in a channel |
| 49 |
my @sub_channels = $sub->channels; |
| 50 |
my @pub_channels = $pub->channels; |
| 51 |
print "Sub is in #moose" if $sub->channels->{'#moose'}; |
| 52 |
print "Pub is in #moose" if $pub->channels->{'#moose'}; |
| 53 |
|
| 54 |
# Raw cache manipulation APIs (not advised; use ->modify instead) |
| 55 |
$bus->lock('channel'); |
| 56 |
$bus->unlock('channel'); |
| 57 |
my @timed_msgs = $bus->fetch('key1', 'key2', 'key3'); |
| 58 |
$bus->store('key', 'value', time, 30); |
| 59 |
|
| 60 |
# Atomic updating of cache content; $_ is stored back on the |
| 61 |
# end of the callback. |
| 62 |
my $rv = $bus->modify('key' => sub { delete $_->{foo} }); |
| 63 |
|
| 64 |
# Shorthand for $bus->modify('key' => sub { $_ = 'val' }); |
| 65 |
$bus->modify('key' => 'val'); |
| 66 |
|
| 67 |
# Shorthand for $bus->modify('key' => sub { $_ }); |
| 68 |
$bus->modify('key'); |
| 69 |
|
| 70 |
# Disconnect the backend connection explicitly |
| 71 |
$bus->disconnect; |
| 72 |
|
| 73 |
DESCRIPTION |
| 74 |
This module provides a simple API for publishing messages to *channels* |
| 75 |
and for subscribing to them. |
| 76 |
|
| 77 |
When a *message* is published on a channel, all subscribers currently in |
| 78 |
that channel will get it on their next "get" or "get_all" call. |
| 79 |
|
| 80 |
Currently, it offers three backends: "DBM_Deep" for on-disk storage, |
| 81 |
"Memcached" for possibly multi-host storage, and "PlainHash" for |
| 82 |
single-process storage. |
| 83 |
|
| 84 |
Please see the tests in t/ for this distribution, as well as "SYNOPSIS" |
| 85 |
above, for some usage examples; detailed documentation is not yet |
| 86 |
available. |
| 87 |
|
| 88 |
SEE ALSO |
| 89 |
IPC::DirQueue, where the subscribers divide the published messages among |
| 90 |
themselves, so different subscribers never see the same message. |
| 91 |
|
| 92 |
AUTHORS |
| 93 |
Audrey Tang <[email protected]> |
| 94 |
|
| 95 |
COPYRIGHT |
| 96 |
Copyright 2006, 2007 by Audrey Tang <[email protected]>. |
| 97 |
|
| 98 |
This software is released under the MIT license cited below. |
| 99 |
|
| 100 |
The "MIT" License |
| 101 |
Permission is hereby granted, free of charge, to any person obtaining a |
| 102 |
copy of this software and associated documentation files (the |
| 103 |
"Software"), to deal in the Software without restriction, including |
| 104 |
without limitation the rights to use, copy, modify, merge, publish, |
| 105 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 106 |
permit persons to whom the Software is furnished to do so, subject to |
| 107 |
the following conditions: |
| 108 |
|
| 109 |
The above copyright notice and this permission notice shall be included |
| 110 |
in all copies or substantial portions of the Software. |
| 111 |
|
| 112 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 113 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 114 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 115 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 116 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 117 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 118 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 119 |
|