AnyEvent::MQTT(3)
π AnyEvent::MQTT(3) β the manual, but make it make sense
okay so welcome to AnyEvent::MQTT(3), bestie π this is the perl doc page for the AnyEvent::MQTT module, aka an AnyEvent module for an MQTT client. yes you read that right. let's get into it π«Ά
β¨ VERSION β¨
version 1.212810 π (yes it's a version number, no it's not a phone number, don't call it)
πΈ SYNOPSIS πΈ
here's the whole vibe in code, literally just copy-paste energy:
use AnyEvent::MQTT;
my $mqtt = AnyEvent::MQTT->new;
my $cv = $mqtt->subscribe(topic => '/topic',
callback => sub {
my ($topic, $message) = @_;
print $topic, ' ', $message, "\n"
});
my $qos = $cv->recv; # subscribed, negotiated QoS == $qos
# publish a simple message
$cv = $mqtt->publish(message => 'simple message',
topic => '/topic');
$cv->recv; # sent
# publish line-by-line from file handle
$cv = $mqtt->publish(handle => \*STDIN,
topic => '/topic');
$cv->recv; # sent
# publish from AnyEvent::Handle
$cv = $mqtt->publish(handle => AnyEvent::Handle->new(my %handle_args),
topic => '/topic');
$cv->recv; # sent
see that $cv->recv? that's you waiting for it to actually go through. the comments are the receipts btw π
π¦ DESCRIPTION π¦
AnyEvent module for MQTT client. that's the whole thing. that's the sentence. π―
IMPORTANT: this is an early release and the API is still subject to change. so likeβ¦ don't build your whole personality on it yet π the vibes are immaculate but the API is not final. no cap.
π METHODS π
π new(%params)
Constructs a new AnyEvent::MQTT object. and like, the supported parameters are (pay attention bestie, there's a lot):
hostβ the server host. defaults to127.0.0.1.portβ the server port. defaults to1883.timeoutβ the timeout for responses from the server.keep_alive_timerβ the keep alive timer.user_nameβ the user name for the MQTT broker.passwordβ the password for the MQTT broker.tlsβ set flag to enable TLS encryption. default is no encryption (yes, plaintext, be aware π).will_topicβ set topic for will message. default isundef, which means no will message will be configured.will_qosβ set QoS for will message. default is'at-most-once'.will_retainβ set retain flag for will message. default is0.will_messageβ set message for will message. default is the empty message.clean_sessionβ set clean session flag for connect message. default is1.client_idβ sets the client id for the client, overriding the default which is"NetMQTTpmNNNNN"whereNNNNNis the current process id.message_log_callbackβ defines a callback to call on every message.on_errorβ defines a callback to call when some error occurs.
two parameters are passed to the callback:
$on_error->($fatal, $message)
where $fatal is a boolean flag and $message is the error message. if the error is fatal, $fatal is true. π¨
handle_argsβ a reference to a list to pass as arguments to theAnyEvent::Handleconstructor (defaults to an empty list reference).
π§ cleanup()
this method attempts to destroy any resources in the event of a disconnection or fatal error. it's giving "closing the tab when the group chat gets too messy" energy π but like, for resources.
π publish( %parameters )
this method is used to publish to a given topic. it returns an AnyEvent condvar which is notified when the publish is complete (written to the kernel or ack'd depending on the QoS level). so you know it's done-done β¨
the parameter hash must include at least a topic value and one of:
messageβ with a string value which is published to the topic,handleβ the value of which must either be anAnyEvent::Handleor will be passed to anAnyEvent::Handleconstructor as thefhargument. thepush_read()method is called on theAnyEvent::Handlewith a callback that will publish each chunk read to the topic.
the parameter hash may also [have] keys for:
qosβ to set the QoS level for published messages (defaultMQTT_QOS_AT_MOST_ONCE),handle_argsβ a reference to a list to pass as arguments to theAnyEvent::Handleconstructor in the final case above (defaults to an empty list reference), orpush_read_argsβ a reference to a list to pass as the arguments to theAnyEvent::Handle#push_readmethod (defaults to['line']to read, and subsequently publish, a line at a time.
β next_message_id()
Returns a 16-bit number to use as the next message id in a message requiring an acknowledgement. little number, big responsibility π«‘
π subscribe( %parameters )
this method subscribes to the given topic. the parameter hash may contain values for the following keys:
topicβ for the topic to subscribe to (this is required),callbackβ for the callback to call with messages (this is required),qosβ QoS level to use (default isMQTT_QOS_AT_MOST_ONCE),cvβAnyEventcondvar to use to signal the subscription is complete. the received value will be the negotiated QoS level.
this method returns the value of the cv parameter if it was supplied, or an AnyEvent condvar created for this purpose. π
πΈ unsubscribe( %parameters )
this method unsubscribes to the given topic. the parameter hash may contain values for the following keys:
topicβ for the topic to unsubscribe from (this is required),callbackβ for the callback to call with messages (this is optional and currently not supported β all callbacks are unsubscribed),cvβAnyEventcondvar to use to signal the unsubscription is complete.
this method returns the value of the cv parameter if it was supplied, or an AnyEvent condvar created for this purpose. π«Ά
heads up: the callback key? optional but also not supported yet. it's giving "coming soon" placeholder energy π all callbacks get unsubscribed regardless.
π₯ "connect( [ $msg ] )"
this method starts the connection to the server. it will be called lazily when required [by] publish or subscribe, so generally it should not be necessary to call it directly. main character energy btw β shows up exactly when needed and not a second earlier π
π― anyevent_read_type()
this method is used to register an AnyEvent::Handle read type method to read MQTT messages. lowkey iconic plumbing ngl.
π DISCLAIMER π
this is not official IBM code. the author works for IBM but wrote this in their spare time (with permission) for fun. fr fr. big "hobby project but make it corporate-adjacent" vibes π
π AUTHOR π
Mark Hindess <soft-cpan@temporalanomaly.com>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2014 by Mark Hindess.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay. π«Ά
and that's the page bestie β¨ this doc was served up by perl v5.42.2, dated 2026-04-15 β same energy as a timestamped selfie, but for a manual page π
πΈ ok bye love u, go write some MQTT πΈ
π¦ where this page comes from
- alpine 3.24: perl-anyevent-mqtt-doc 1.212810-r2 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms