Sign in to PrePAN

AnyEvent::Net::MPD A non-blocking interface for MPD



use AnyEvent::Net::MPD;

my $mpd = AnyEvent::Net::MPD->new( host => $ARGV[0] )->connect;

my @subsystems = qw( player mixer database );

# Register a listener
foreach my $subsystem (@subsystems) {
  $mpd->on( $subsystem => sub {
    my ($self) = @_;
    print "$subsystem has changed\n";

    # Stop listening if mixer changes
    $mpd->noidle if $subsystem eq 'mixer';

# Send a command
my $stats = $mpd->send( 'stats' );

# Or in blocking mode
my $status = $mpd->send( 'status' )->recv;

# Which is the same as
$status = $mpd->get( 'status' );

print "Server is ", $status->{state}, " state\n";
print "Server has ", $stats->recv->{albums}, " albums in the database\n";

# Put the client in looping idle mode
my $idle = $mpd->idle( @subsystems );

# Set the emitter in motion, until the next call to noidle


This is a module to asynchronously talk to an MPD server.

There are a number of MPD libraries on CPAN, but all of them are either blocking (eg. Audio::MPD, Net::MPD) or plugins that depend on a larger framework (eg. POE::Component::Client::MPD, Dancer::Plugin::MPD). Of these, Audio::MPD seems unmaintained, and the POE component depends on Audio::MPD as well.

Net::MPD states an asynchronous interface in its feature wishlist, but it hasn't seen any updates in about a year. An empty repository for Net::MPD::Async exists on the author's Github page, so it doesn't seem like any work has been done on that front.

That was the motivation to write this one.

Apart from a review of the code itself, I would be grateful for any comments on the namespace. I wouldn't want to put this under either Audio::MPD or Net::MPD, since the interface of this module is not like any of those, so it's not really in the same family. For the time being, I chose to go with AnyEvent::Net::MPD since the AnyEvent::Net namespace already seems to have other libraries for the purpose of talking to specific networked services.

There are some example scripts in the t/examples folder.

All comments are most welcome.


Please sign up to post a review.