Created
July 21, 2011 15:02
-
-
Save mythosil/1097379 to your computer and use it in GitHub Desktop.
AnyEvent::Redis pubsub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use AnyEvent::Handle; | |
use AnyEvent::Redis; | |
my $redis_sub = AnyEvent::Redis->new( | |
host => '127.0.0.1', | |
port => 6379, | |
encoding => 'utf8', | |
on_error => sub { warn @_ }, | |
); | |
my $redis_pub = AnyEvent::Redis->new( | |
host => '127.0.0.1', | |
port => 6379, | |
encoding => 'utf8', | |
on_error => sub { warn @_ }, | |
); | |
my $channel = "subscribe_test"; | |
my $cv_sub = $redis_sub->subscribe($channel, sub { | |
my ($message, $channel) = @_; | |
print "[$channel] $message\n"; | |
}); | |
$cv_sub->cb(sub { | |
$cv_sub->recv; | |
}); | |
my $cv = AE::cv; | |
my $handle = AnyEvent::Handle->new( | |
fh => \*STDIN, | |
on_read => sub { | |
shift->push_read( | |
line => sub { | |
my ($h, $line) = @_; | |
my $cv_pub = $redis_pub->publish($channel, $line); | |
$cv_pub->cb(sub { | |
$cv_pub->recv; | |
}); | |
}, | |
); | |
}, | |
on_eof => sub { | |
my ($h) = @_; | |
warn "got eof"; | |
$h->destroy; | |
$cv->send; | |
}, | |
on_error => sub { | |
my ($h, $fatal, $msg) = @_; | |
warn "got error $msg"; | |
$h->destroy; | |
$cv->send; | |
}, | |
); | |
$cv->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment