=head1 NAME AnyEvent::Stomper - Flexible non-blocking STOMP client =head1 SYNOPSIS use AnyEvent; use AnyEvent::Stomper; my $stomper = AnyEvent::Stomper->new( host => 'localhost', prot => '61613', login => 'guest', passcode => 'guest', ); my $cv = AE::cv; $stomper->subscribe( id => 'foo', destination => '/queue/foo', on_receipt => sub { my $err = $_[1]; if ( defined $err ) { warn $err->message . "\n"; $cv->send; return; } $stomper->send( destination => '/queue/foo', body => 'Hello, world!', ); }, on_message => sub { my $msg = shift; my $body = $msg->body; print "Consumed: $body\n"; $cv->send; }, ); $cv->recv; =head1 DESCRIPTION AnyEvent::Stomper is flexible non-blocking STOMP client. Supports following STOMP versions: 1.0, 1.1, 1.2. Is recommended to read STOMP protocol specification before using the client: L =head1 CONSTRUCTOR =head2 new( %params ) my $stomper = AnyEvent::Stomper->new( host => 'localhost', port => '61613', login => 'guest', passcode => 'guest', vhost => '/', heartbeat => [ 5000, 5000 ], connection_timeout => 5, lazy => 1, reconnect_interval => 5, on_connect => sub { # handling... }, on_disconnect => sub { # handling... }, on_error => sub { my $err = shift; # error handling... }, ); =over =item host => $host Server hostname (default: localhost) =item port => $port Server port (default: 61613) =item login => $login The user identifier used to authenticate against a secured STOMP server. =item passcode => $passcode The password used to authenticate against a secured STOMP server. =item vhost => $vhost The name of a virtual host that the client wishes to connect to. =item heartbeat => \@heartbeat Heart-beating can optionally be used to test the healthiness of the underlying TCP connection and to make sure that the remote end is alive and kicking. The first number sets interval in milliseconds between outgoing heart-beats to the STOMP server. C<0> means, that the client will not send heart-beats. The second number sets interval in milliseconds between incoming heart-beats from the STOMP server. C<0> means, that the client does not want to receive heart-beats. heartbeat => [ 5000, 5000 ], Not set by default. =item connection_timeout => $connection_timeout Specifies connection timeout. If the client could not connect to the server after specified timeout, the C callback is called with the C error. The timeout specifies in seconds and can contain a fractional part. connection_timeout => 10.5, By default the client use kernel's connection timeout. =item lazy => $boolean If enabled, the connection establishes at time when you will send the first command to the server. By default the connection establishes after calling of the C method. Disabled by default. =item reconnect_interval => $reconnect_interval If the connection to the server was lost, the client will try to restore the connection when you execute next command. By default reconnection is performed immediately, on next command execution. If the C parameter is specified, the client will try to reconnect only after this interval and commands executed between reconnections will be queued. The client will try to reconnect only once and, if attempt fails, the error object is passed to command callback. If you need several attempts of the reconnection, you must retry a command from the callback as many times, as you need. reconnect_interval => 5, Not set by default. =item handle_params => \%params Specifies L parameters. handle_params => { autocork => 1, linger => 60, } Enabling of the C parameter can improve performance. See documentation on L for more information. =item default_headers => \%headers Specifies default headers for all outgoing frames. default_headers => { 'x-foo' => 'foo_value', 'x-bar' => 'bar_value', } =item command_headers Specifies default headers for particular commands. command_headers => { SEND => { receipt => 'auto', }, SUBSCRIBE => { durable => 'true', ack => 'client', }, } =item on_connect => $cb->() The C callback is called when the connection is successfully established. Not set by default. =item on_disconnect => $cb->() The C callback is called when the connection is closed by any reason. Not set by default. =item on_error => $cb->( $err ) The C callback is called when occurred an error, which was affected on entire client (e. g. connection error or authentication error). Also the C callback is called on command errors if the command callback is not specified. If the C callback is not specified, the client just print an error messages to C. =back =head1 COMMAND METHODS To execute the STOMP command you must call appropriate method. STOMP headers can be specified as command parameters. The client automatically adds C header to all outgoing frames. Every command method can also accept two additional parameters: the C parameter where you can specify the body of the frame, and the C parameter that is the alternative way to specify the command callback. If you want to receive C frame, you must specify C header. The C header can take the special value C. If it set, the receipt identifier will be generated automatically by the client. The C frame is passed to the command callback in first argument as the object of the class L. If the C header is not specified the first argument of the command callback will be C. For commands C, C, C the client automatically adds C header for internal usage. The command callback is called in one of two cases depending on the presence of the C header. First case, when the command was successfully sent to the server. Second case, when the C frame will be received. If any error occurred during the command execution, the error object is passed to the callback in second argument. Error object is the instance of the class L. The command callback is optional. If it is not specified and any error occurred, the C callback of the client is called. The full list of all available headers for every command you can find in STOMP protocol specification and in documentation on your STOMP server. For various versions of STOMP protocol and various STOMP servers they can be differ. =head2 send( [ %params ] [, $cb->( $receipt, $err ) ] ) Sends a message to a destination in the messaging system. $stomper->send( destination => '/queue/foo', body => 'Hello, world!', ); $stomper->send( destination => '/queue/foo', body => 'Hello, world!', sub { my $err = $_[1]; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; my $err_frame = $err->frame; # error handling... return; } } ); $stomper->send( destination => '/queue/foo', receipt => 'auto', body => 'Hello, world!', on_receipt => sub { my $receipt = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; my $err_frame = $err->frame; # error handling... return; } # receipt handling... } ); =head2 subscribe( [ %params ] [, $cb->( $msg ) ] ) The method is used to register to listen to a given destination. The C method require the C callback, which is called on every received C frame from the server. The C frame is passed to the C callback in first argument as the object of the class L. If the C method is called with one callback, this callback will be act as C callback. $stomper->subscribe( id => 'foo', destination => '/queue/foo', sub { my $msg = shift; my $headers = $msg->headers; my $body = $msg->body; # message handling... }, ); $stomper->subscribe( id => 'foo', destination => '/queue/foo', ack => 'client', on_receipt => sub { my $receipt = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; my $err_frame = $err->frame; return; } # receipt handling... }, on_message => sub { my $msg = shift; my $headers = $msg->headers; my $body = $msg->body; # message handling... }, ); =head2 unsubscribe( [ %params ] [, $cb->( $receipt, $err ) ] ) The method is used to remove an existing subscription. $stomper->unsubscribe( id => 'foo', destination => '/queue/foo', sub { my $receipt = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; my $err_frame = $err->frame; return; } # receipt handling... } ); =head2 ack( [ %params ] [, $cb->( $receipt, $err ) ] ) The method is used to acknowledge consumption of a message from a subscription using C or C acknowledgment. Any messages received from such a subscription will not be considered to have been consumed until the message has been acknowledged via an C method. $stomper->ack( id => $ack_id ); $stomper->ack( id => $ack_id, receipt => 'auto', sub { my $receipt = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; my $err_frame = $err->frame; # error handling... } # receipt handling... } ); =head2 nack( [ %params ] [, $cb->( $receipt, $err ) ] ) The C method is the opposite of C method. It is used to tell the server that the client did not consume the message. $stomper->nack( id => $ack_id ); $stomper->nack( id => $ack_id, receipt => 'auto', sub { my $receipt = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; my $err_frame = $err->frame; # error handling... } # receipt handling... } ); =head2 begin( [ %params ] [, $cb->( $receipt, $err ) ] ) The method C is used to start a transaction. =head2 commit( [ %params ] [, $cb->( $receipt, $err ) ] ) The method C is used to commit a transaction. =head2 abort( [ %params ] [, $cb->( $receipt, $err ) ] ) The method C is used to roll back a transaction. =head2 disconnect( [ %params ] [, $cb->( $receipt, $err ) ] ) A client can disconnect from the server at anytime by closing the socket but there is no guarantee that the previously sent frames have been received by the server. To do a graceful shutdown, where the client is assured that all previous frames have been received by the server, you must call C method and wait for the C frame. =head2 execute( $command, [ %params ] [, $cb->( $receipt, $err ) ] ) An alternative method to execute commands. In some cases it can be more convenient. $stomper->execute( 'SEND', destination => '/queue/foo', receipt => 'auto', body => 'Hello, world!', sub { my $receipt = shift; my $err = shift; if ( defined $err ) { my $err_msg = $err->message; my $err_code = $err->code; my $err_frame = $err->frame; # error handling... return; } # receipt handling... } ); =head1 ERROR CODES Every error object, passed to callback, contain error code, which can be used for programmatic handling of errors. AnyEvent::Stomper provides constants for error codes. They can be imported and used in expressions. use AnyEvent::Stomper qw( :err_codes ); =over =item E_CANT_CONN Can't connect to the server. All operations were aborted. =item E_IO Input/Output operation error. The connection to the STOMP server was closed and all operations were aborted. =item E_CONN_CLOSED_BY_REMOTE_HOST The connection closed by remote host. All operations were aborted. =item E_CONN_CLOSED_BY_CLIENT Connection closed by client prematurely. Uncompleted operations were aborted =item E_OPRN_ERROR Operation error. For example, missing required header. =item E_UNEXPECTED_DATA The client received unexpected data from the server. The connection to the STOMP server was closed and all operations were aborted. =item E_READ_TIMEDOUT Read timed out. The connection to the STOMP server was closed and all operations were aborted. =back =head1 OTHER METHODS =head2 host() Gets current host of the client. =head2 port() Gets current port of the client. =head2 connection_timeout( [ $fractional_seconds ] ) Gets or sets the C of the client. The C value resets the C to default value. =head2 reconnect_interval( [ $fractional_seconds ] ) Gets or sets C of the client. =head2 on_connect( [ $callback ] ) Gets or sets the C callback. =head2 on_disconnect( [ $callback ] ) Gets or sets the C callback. =head2 on_error( [ $callback ] ) Gets or sets the C callback. =head2 force_disconnect() The method for forced disconnection. All uncompleted operations will be aborted. =head1 WORKING WITH CLUSTER If you have the cluster of STOMP servers, you can use L to work with it. =head1 SEE ALSO L =head1 AUTHOR Eugene Ponizovsky, Eponizovsky@gmail.comE Sponsored by SMS Online, Edev.opensource@sms-online.comE =head1 COPYRIGHT AND LICENSE Copyright (c) 2016-2017, Eugene Ponizovsky, SMS Online. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.