AnyEvent-STOMP

 view release on metacpan or  search on metacpan

lib/AnyEvent/STOMP.pm  view on Meta::CPAN

package AnyEvent::STOMP;

use 5.008;
use common::sense;

use base 'Object::Event';
use Carp qw(croak);
use AnyEvent;
use AnyEvent::Handle;

our $VERSION = 0.7;

=head1 NAME

AnyEvent::STOMP - A lightweight event-driven STOMP client

=head1 SYNOPSIS

 use AnyEvent;
 use AnyEvent::STOMP;

 my $client = AnyEvent::STOMP->connect($host, $port, $ssl, $destination, $ack,
                                       { connect_headers },
                                       { subscribe_headers });

 $client->send($command, $headers, $body);

 # Register interest in new messages
 $client->reg_cb(MESSAGE => sub {
     my (undef, $body, $headers) = @_;
     # Do something with the frame
 });

 # Register interest in any frame received.  Use with caution.
 $client->reg_cb(frame => sub {
     my (undef, $type, $body, $headers) = @_;
     # Do something with the frame
 });

 # Start the event loop
 AnyEvent->condvar->recv;

=cut

=head1 DESCRIPTION

AnyEvent::STOMP is a lightweight event-driven STOMP client.  It's intended
to be run in the context of an event loop driven by AnyEvent.

=head2 Making a connection

 my $client = AnyEvent::STOMP->connect($host, $port, $ssl, $destination, $ack,
                                       { connect_headers },
                                       { subscribe_headers });

Only the first parameter (the hostname) is required.  The remaining optional
arguments are:

=over

=item port

The port number to connect to.  If not specified, defaults to 61612 (if SSL/TLS
is used) or 61613 (if not).

=item ssl

If set to a true value, use SSL/TLS to connect.

=item destination

If defined, subscribe to the specified destination (queue) upon connection.

=item ack

Sets the behavior with respect to STOMP frame acknowledgments.

If this value is 0 or undef, no acknowledgment is required: the server will
consider all sent frames to be delivered, regardless of whether the client has
actually received them.  (This is the default behavior according to the STOMP
protocol.)

If set to C<auto>, the client will automatically acknowledge a frame upon
receipt.

If set to C<manual>, the caller must acknowledge frames manually via the
ack() method.

=item connect_headers

An anonymous hash of headers (key/value pairs) to send in the STOMP CONNECT
frame.

=item subscribe_headers

An anonymous hash of headers (key/value pairs) to send in the STOMP SUBSCRIBE
frame.

=back

=cut

sub connect {
    my $class = shift;
    my ($host, $port, $ssl, $destination, $ack,
        $connect_headers, $subscribe_headers) = @_;

    croak 'No host provided' unless $host;
    croak "ack value must be 0, undef, 'auto' or 'manual'"
        if $ack && $ack ne 'auto' && $ack ne 'manual';

    my $self = $class->SUPER::new;

    $self->{ack} = $ack;

    $port ||= ($ssl ? 61612 : 61613);

    my $connect_cb;
    $self->{handle} = AnyEvent::Handle->new(
        connect => [ $host, $port ],
        tls => $ssl ? 'connect' : undef,
        keepalive => 1,
        on_prepare => sub { $self->event('prepare', @_); },
        on_connect => sub {
            $self->event('connect', @_);
            $self->send_frame('CONNECT', undef, $connect_headers);
            if ($destination) {
                $subscribe_headers->{destination} = $destination;
                $subscribe_headers->{ack} = 'client' if $ack;
                $connect_cb = $self->reg_cb(CONNECTED => sub {
                        $self->{session_id} = $_[2]->{session};
                        $self->send_frame('SUBSCRIBE',
                            undef, $subscribe_headers);
                        undef $connect_cb;
                });
            }
        },
        on_connect_error => sub {
            $self->event('connect_error', $_[1]);
        },
        on_error => sub {
            $self->unreg_cb($connect_cb) if (defined $connect_cb);
            $self->{handle}->destroy;
            $self->event('io_error', $_[2]);
        },
        on_read => sub { $self->_receive_frame },
    );
    return bless($self, $class);
}

=head2 Sending a message

To send a message, just call send() with the body, the destination (queue)
name, and (optionally) any additional headers:

 $client->send($body, $destination, $headers); # headers may be undef

=cut

sub send {
    my $self = shift;
    my ($body, $destination, $headers) = @_;

    croak 'Missing destination' unless defined $destination;

    $headers->{destination} = $destination;
    $self->send_frame('SEND', $body, $headers);
}

=head2 Sending STOMP frames

You can also send arbitrary STOMP frames:

 $client->send_frame($command, $body, $headers); # headers may be undef

See the STOMP protocol documentation for more details on valid commands and
headers.

=head3 Content Length

The C<content-length> header is special because it is sometimes used to



( run in 0.948 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )