Net::MQTT::Message(3)

πŸ“¦ version 1.163170, in alpine 3.24

πŸ’– Net::MQTT::Message β€” the Perl module that's literally your MQTT bestie πŸ’–

Okay so bestie, welcome to Net::MQTT::Message(3) β€” the User Contributed Perl Documentation page for a module that lowkey does one thing and does it iconically: it wraps up a single MQTT message so you can ship it to a socket or scoop one back out. Slay. ✨


🌸 VERSION 🌸

version 1.163170 β€” no cap, that's the one.


πŸ¦‹ SYNOPSIS πŸ¦‹

So like, here's how you actually use it. Copy it exactly, don't @ me:

  use Net::MQTT::Constants;
  use Net::MQTT::Message;
  use IO::Socket::INET;
  my $socket = IO::Socket::INET->new(PeerAddr => '127.0.0.1:1883');
  my $mqtt = Net::MQTT::Message->new(message_type => MQTT_CONNECT);
  print $socket $mqtt->bytes;

  my $tcp_payload = pack 'H*', '300d000774657374696e6774657374';
  $mqtt = Net::MQTT::Message->new_from_bytes($tcp_payload);
  print 'Received: ', $mqtt->string, "\n";

🫢 See? Beautiful. Build a message, yeet it at the socket, then parse one back and print a little summary like the main character you are.


✨ DESCRIPTION ✨

This module encapsulates a single MQTT message β€” like, one (1) vibe, one (1) packet. It's got subclasses that each represent a specific message type, because apparently we're doing object-oriented gatekeeping and honestly? It works. πŸ‘‘


πŸŽ€ METHODS πŸŽ€

new( %parameters ) β€” the constructor, the moment, the icon

Constructs a Net::MQTT::Message object from the parameters you hand it. The common parameter keys are:

  • message_type β€” the message type field of the MQTT message. This should be an integer between 0 and 15 inclusive. The module Net::MQTT::Constants provides constants you can use for this value. πŸ’– This parameter is required. (Fr, you cannot skip it.)
  • dup β€” the duplicate flag field of the MQTT message. Should be either 1 or 0. The default is 0.
  • qos β€” the QoS field of the MQTT message. Should be an integer between 0 and 3 inclusive. The default is as specified in the spec or 0 ("at most once") otherwise. The module Net::MQTT::Constants provides constants you can use for this value.
  • retain β€” the retain flag field of the MQTT message. Should be either 1 or 0. The default is 0.

The remaining keys are dependent on the specific message type. 🧚 The documentation for the subclasses for each message type lists methods with the same name as the required keys.


new_from_bytes( $packed_bytes, [ $splice ] ) β€” glow-up from raw bytes

Attempts to construct a Net::MQTT::Message object based on the given packed byte string. If there are insufficient bytes, then undef is returned. 😭 (Rude but fair.)

If the splice parameter is provided and true, then the processed bytes are removed from the scalar referenced by the $packed_bytes parameter. πŸ‘€ Very efficient, very mindful.


message_type() β€” what's your type, bestie?

Returns the message type field of the MQTT message. The module Net::MQTT::Constants provides a function, message_type_string, that can be used to convert this value to a human-readable string. 🌈


dup() β€” the duplicate flag, no more no less

The duplicate flag field of the MQTT message.


qos() β€” quality of service, quality of life

The QoS field of the MQTT message. The module Net::MQTT::Constants provides a function, qos_string, that can be used to convert this value to a human-readable string. πŸ’…


retain() β€” keep it, queen

The retain field of the MQTT message.


remaining() β€” the leftover crumbs πŸ“

This contains a packed string of bytes with any of the payload of the MQTT message that was not parsed by these modules. This should not be required for packets that strictly follow the standard.


string([ $prefix ]) β€” the logging summarize moment

Returns a summary of the message as a string, suitable for logging. If provided, each line will be prefixed by the optional $prefix. So cute, so organized. πŸŽ€


bytes() β€” the actual raw material

Returns the bytes of the message, suitable for writing to a socket. Slap it into your print $socket ... and go. πŸ”₯


πŸ‘‘ AUTHOR πŸ‘‘

Mark Hindess <soft-cpan@temporalanomaly.com> β€” the mastermind, the moment.


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.


Okay bestie, that's the whole page β€” every flag, every method, every default, every name, all intact. 🫢 This was perl v5.42.2 and the page date is 2026-02-15 β€” so like, mark your calendars and go write some MQTT messages. You're gonna slay. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from