Net::STOMP::Client(3)
π NAME β Net::STOMP::Client
Okay bestie, welcome to the Net::STOMP::Client(3) page from User Contributed Perl Documentation. This is literally the STOMP object oriented client module β thatβs the whole NAME vibe, and weβre about to unpack it. π
π SYNOPSIS
Hereβs the whole thing in code, no cap:
#
# simple producer
#
use Net::STOMP::Client;
$stomp = Net::STOMP::Client->new(host => "127.0.0.1", port => 61613);
$stomp->connect(login => "guest", passcode => "guest");
$stomp->send(destination => "/queue/test", body => "hello world!");
$stomp->disconnect();
#
# consumer with client side acknowledgment
#
use Net::STOMP::Client;
$stomp = Net::STOMP::Client->new(host => "127.0.0.1", port => 61613);
$stomp->connect(login => "guest", passcode => "guest");
# declare a callback to be called for each received message frame
$stomp->message_callback(sub {
my($self, $frame) = @_;
$self->ack(frame => $frame);
printf("received: %s\n", $frame->body());
return($self);
});
# subscribe to the given queue
$stomp->subscribe(
destination => "/queue/test",
id => "testsub", # required in STOMP 1.1
ack => "client", # client side acknowledgment
);
# wait for a specified message frame
$stomp->wait_for_frames(callback => sub {
my($self, $frame) = @_;
if ($frame->command() eq "MESSAGE") {
# stop waiting for new frames if body is "quit"
return(1) if $frame->body() eq "quit";
}
# continue to wait for more frames
return(0);
});
$stomp->unsubscribe(id => "testsub");
$stomp->disconnect();
π¦ DESCRIPTION
This module gives you an object oriented client interface to interact with servers that support STOMP β aka Streaming Text Orientated Messaging Protocol. It supports the major features of modern messaging brokers: SSL, asynchronous I/O, receipts and transactions. Slay. π
π CONSTRUCTOR
The new() method can be used to create a Net::STOMP::Client object that will later be used to interact with a server. The following attributes are supported:
accept_versionβ the STOMP version to use (string) or versions to use (reference to a list of strings); this defaults to the list of all supported versions; seeNet::STOMP::Client::Versionfor more informationversionβ this attribute is obsolete and should not be used anymore, useaccept_versioninstead; it is left here only to provide backward compatibility withNet::STOMP::Client1.xuriβ the Uniform Resource Identifier (URI) specifying where the STOMP service is and how to connect to it, this can be for instancetcp://msg01:6163or something more complex, seeNet::STOMP::Client::Connectionfor more informationhostβ the server name or IP addressportβ the port number of the STOMP serviceauthβ the authentication credential(s) to use, seeNet::STOMP::Client::Authfor more informationsockoptsβ arbitrary socket options (as a hash reference) that will be passed toIO::Socket::INET->new()orIO::Socket::SSL->new()client_heart_beatβ the desired client-side heart-beat setting, seeNet::STOMP::Client::HeartBeatfor more informationserver_heart_beatβ the desired server-side heart-beat setting, seeNet::STOMP::Client::HeartBeatfor more informationdebugβ the debugging flags for this object, see the βDEBUGGINGβ section for more informationtimeoutβ the maximum time (in seconds) for various operations, see the βTIMEOUTSβ section for more information
Upon object creation, a TCP connection is made to the server but no data (i.e. STOMP frame) is exchanged. π«Ά
π DEBUGGING
Net::STOMP::Client uses No::Worries::Logβs log_debug() to log debugging information. In addition, to avoid useless data massaging, it also uses a debug string to specify what will be logged using log_debug().
The debug string should contain a list of words describing what to log. For instance, io logs I/O information while io connection logs both I/O and connection information.
Here are the supported debug words that can be used:
allβ everythingapiβ high-level API callsbodyβ frame bodiescommandβ frame commandsconnectionβ connection establishmentheaderβ frame headersioβ I/O as bytes sent/received
To enable debugging, you must first configure No::Worries::Log so that it indeed reports debugging messages. This can be done with something like:
log_filter("debug");
or, to enable logging only from Net::STOMP::Client modules:
log_filter("debug caller=~^Net::STOMP::Client");
See the No::Worries::Log documentation for more information.
Then, you have to tell Net::STOMP::Client to indeed log what you want to see. This can be done globally for all connections by setting the global variable $Net::STOMP::Client::Debug:
$Net::STOMP::Client::Debug = "connection api";
or per connection via the new() method:
$stomp = Net::STOMP::Client->new(
uri => "stomp://mybroker:6163",
debug => "connection api",
);
β±οΈ TIMEOUTS
By default, when sending STOMP frames, the module waits until the frame indeed has been sent (from the socket point of view). In case the server is stuck or unusable, the module can therefore hang. π
When creating the Net::STOMP::Client object, you can pass a timeout attribute to better control how certain operations handle timeouts.
This attribute should contain a reference to hash with the following keys:
connectβ TCP-level timeout that will be given to the underlyingIO::Socket::INETorIO::Socket::SSLobject (default: none)connectedβ timeout used while waiting for the initialCONNECTEDframe from the broker (default: 10)disconnectβ timeout specifying how long thedisconnect()method should wait for aRECEIPTframe back in case theDISCONNECTframe contained a receipt (default: 10)receiveβ timeout used while trying to receive any frame (default: none)sendβ timeout used while trying to send any frame (default: none)
All values are in seconds. No timeout means wait until the operation succeeds.
As a shortcut, the timeout attribute can also be a scalar. In this case, only the connect and connected operations use this value.
π STOMP METHODS
With a Net::STOMP::Client object, the following methods can be used to interact with the server. They match one-to-one the different commands that a client frame can hold:
connect()β connect to serverdisconnect()β disconnect from serversubscribe()β subscribe to somethingunsubscribe()β unsubscribe from somethingsend()β send a message somewhereack()β acknowledge the reception of a messagenack()β acknowledge the rejection of a message (STOMP >=1.1 only)begin()β begin/start a transactioncommit()β commit a transactionabort()β abort/rollback a transaction
All these methods can receive options that will be passed directly as frame headers. For instance:
$stomp->subscribe(
destination => "/queue/test",
id => "testsub",
ack => "client",
);
Some methods also support additional options:
send()βbodyorbody_reference: holds the body or body reference of the message to be sentack()βframe: holds theMESSAGEframe object to acknack()βframe: holds theMESSAGEframe object to nack
Finally, all methods support debug and timeout options that will be given to the send_frame() method called internally to send the crafted frame.
π OTHER METHODS
In addition to the STOMP methods, the following ones are also available:
new(OPTIONS)β return a newNet::STOMP::Clientobject (constructor)peer()β return aNet::STOMP::Client::Peerobject containing information about the connected STOMP serversocket()β return the file handle of the socket connecting the client and the serverserver()β return the server header seen on theCONNECTEDframe (if any)session()β return the session identifier if connected or false otherwiseuuid()β return a universal pseudo-unique identifier to be used for instance in receipts and transactionswait_for_frames()β wait for frames coming from the server, see the next section for more informationnoop([timeout => TIMEOUT])β send an empty/noop frame i.e. a single newline byte, usingsend_frame()underneath
π CALLBACKS π
okay so bestie, STOMP is asynchronous β like, the server can just pop a MESSAGE frame at you whenever it wants, no warning, no text-back. So Net::STOMP::Client handles frames with callbacks. And there are literally two levels of them. π
First level: per-command callbacks. These get called every time a frame is handled, via the internal dispatch_frame() method. Net::STOMP::Client ships default callbacks that should be enough for every frame except MESSAGE frames β those should really be handled by the coder. These callbacks should return undef on error, and something else on success. Slay. β¨
Hereβs the example where a callback counts the messages received:
$stomp->message_callback(sub {
my($self, $frame) = @_;
$MessageCount++;
return($self);
});
Here are the methods you use to get or set these per-command callbacks:
connected_callback([SUBREF])error_callback([SUBREF])message_callback([SUBREF])receipt_callback([SUBREF])
These callbacks are somehow global, and it is good practice not to change them during a session. If you do not need a global message callback, you can supply the dummy:
$stomp->message_callback(sub { return(1) });
Then thereβs wait_for_frames(). It takes an optional callback argument holding some code to be called for each received frame β after the per-command callback has been called. Think of this as a local callback, only valid for the call to wait_for_frames(). This callback must return undef on error, false if more frames are expected, or true if wait_for_frames() can now stop waiting for new frames and return. iykyk π
Here are all the options that can be given to wait_for_frames():
callbackβ code to be called for each received frame (see above)timeoutβ time to wait before giving up;undefmeans wait forever, and this is the defaultonceβ wait only for one frame, within the given timeout
The return value of wait_for_frames() can be: false if no suitable frame has been received, the received frame if there is no user callback, or the user callback return value otherwise. Periodt. π«Ά
π TRANSACTIONS π
Here is an example using transactions:
# create a unique transaction id
$tid = $stomp->uuid();
# begin the transaction
$stomp->begin(transaction => $tid);
# send two messages as part of this transaction
$stomp->send(
destination => "/queue/test1",
body => "message 1",
transaction => $tid,
);
$stomp->send(
destination => "/queue/test2",
body => "message 2",
transaction => $tid,
);
# commit the transaction
$stomp->commit(transaction => $tid);
literally iconic. π₯
π¦ LOW-LEVEL API π¦
It should be enough to use the high-level API and use, for instance, the send() method to create a "MESSAGE" frame and send it in one go. Easy. β¨
If you need lower-level interaction, you can manipulate frames with the Net::STOMP::Client::Frame module.
You can also use:
$stomp->dispatch_frame(FRAME, [OPTIONS])β dispatch one received frame by calling the appropriate callback; supported options:"debug"$stomp->send_frame(FRAME, [OPTIONS])β try to send the given frame object; supported options:"timeout"and"debug"$stomp->send_message(MESSAGE, [OPTIONS])β identical tosend_frame()but taking aMessaging::Messageobject$stomp->queue_frame(FRAME, [OPTIONS])β add the given frame to the outgoing buffer queue; supported options:"debug"$stomp->queue_message(MESSAGE, [OPTIONS])β identical toqueue_frame()but taking aMessaging::Messageobject$stomp->send_data([OPTIONS])β send all the queued data; supported options:"timeout"and"debug"$stomp->receive_frame([OPTIONS])β try to receive a frame; supported options:"timeout"and"debug"$stomp->receive_data([OPTIONS])β try to receive data (this data will be appended to the incoming buffer); supported options:"timeout"and"debug"$stomp->outgoing_buffer_length()β return the length (in bytes) of the outgoing buffer$stomp->incoming_buffer_reference()β return a reference to the incoming buffer
In these methods, the "timeout" option can either be undef (meaning block until itβs done), or 0 (meaning do not block at all), or a positive number (meaning block at most this number of seconds). choose your fighter. π
π COMPATIBILITY π
This module has been successfully tested against ActiveMQ, Apollo, HornetQ and RabbitMQ brokers. No cap. π
See Net::STOMP::Client::Version for the list of supported STOMP protocol versions.
β SEE ALSO β
Messaging::MessageNet::STOMP::Client::AuthNet::STOMP::Client::ConnectionNet::STOMP::Client::FrameNet::STOMP::Client::HeartBeatNet::STOMP::Client::PeerNet::STOMP::Client::ReceiptNet::STOMP::Client::TutorialNet::STOMP::Client::VersionNo::Worries::Log
π AUTHOR π
Lionel Cons <http://cern.ch/lionel.cons>
Copyright (C) CERN 2010-2017
π and thatβs the whole vibe, bestie! This page was rendered by perl v5.32.1 on 2019-07-26, and the manual page itself is Net::STOMP::Client(3). Go off. π
β¨
π¦ where this page comes from
- epel 9: perl-Net-STOMP-Client 2.3-10.el9 Β· GPL+ or Artistic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms