Messaging::Message(3)
π NAME π
Okay bestie, welcome to Messaging::Message(3) from the User Contributed Perl Documentation π
This is Messaging::Message β literally the abstraction of a message, and it's giving main-character energy for messaging systems β¨
πΈ SYNOPSIS πΈ
Here's the whole vibe in code, no cap:
use Messaging::Message;
# constructor + setters
$msg = Messaging::Message->new();
$msg->body("hello world");
$msg->header({ subject => "test" });
$msg->header_field("message-id", 123);
# fancy constructor
$msg = Messaging::Message->new(
body => "hello world",
header => {
"subject" => "test",
"message-id" => 123,
},
);
# getters
if ($msg->body() =~ /something/) {
...
}
$id = $msg->header_field("message-id");
π¦ DESCRIPTION π¦
This module gives you an abstraction of a "message", like the ones used in messaging β for instance, peek http://en.wikipedia.org/wiki/Enterprise_messaging_system π
There's also a Python implementation of the same messaging abstractions at https://github.com/cern-mig/python-messaging, so messaging components can be written in different programming languages. Slay, interoperability π
A message consists of header fields (collectively called "the header of the message") and a body.
Each header field is a key/value pair where the key and the value are text strings. The key is unique within the header, so we can use a hash table to represent the header of the message.
The body is either a text string or a binary string. This distinction is needed because text may need to be encoded (for instance using UTF-8) before being stored on disk or sent across the network.
To make things clear:
- a text string (aka character string) is a sequence of Unicode characters
- a binary string (aka byte string) is a sequence of bytes
Both the header and the body can be empty. Periodt.
π JSON MAPPING π
In order to ease message manipulation (e.g. exchanging between applications, maybe written in different programming languages), we define here a standard mapping between a Messaging::Message object and a JSON object.
A message as defined above naturally maps to a JSON object with the following fields:
headerβ the message header as a JSON object (with all values being JSON strings)bodyβ the message body as a JSON stringtextβ a JSON boolean specifying whether the body is text string (as opposed to binary string) or notencodingβ a JSON string describing how the body has been encoded (see below)
All fields are optional and default to empty/false if not present.
Since JSON strings are text strings (they can contain any Unicode character), the message header directly maps to a JSON object. There is no need to use encoding here.
For the message body, this is more complex. A text body can be put as-is in the JSON object, but a binary body must be encoded beforehand because JSON does not handle binary strings. Additionally, we want to allow body compression in order to optionally save space. This is where the encoding field comes into play.
The encoding field describes which transformations have been applied to the message body. It is a "+" separated list of transformations that can be:
"base64"β Base64 encoding (for binary body or compressed body)"utf8"β UTF-8 encoding (only needed for a compressed text body)"lz4"or"snappy"or"zlib"β LZ4 or Snappy or Zlib compression (only one can be specified)
Here is for instance the JSON object representing an empty message (i.e. the result of Messaging::Message->new()):
{}
Here is a more complex example, with a binary body:
{
"header":{"subject":"demo","destination":"/topic/test"},
"body":"YWJj7g==",
"encoding":"base64"
}
You can use the jsonify() method to convert a Messaging::Message object into a hash reference representing the equivalent JSON object.
Conversely, you can create a new Messaging::Message object from a compatible JSON object (again, a hash reference) with the dejsonify() method.
Using this JSON mapping of messages is very convenient because you can easily put messages in larger JSON data structures. You can for instance store several messages together using a JSON array of these messages.
Here is for instance how you could construct a message containing in its body another message along with error information:
use JSON qw(to_json);
# get a message from somewhere...
$msg1 = ...;
# jsonify it and put it into a simple structure
$body = {
message => $msg1->jsonify(),
error => "an error message",
time => time(),
};
# create a new message with this body
$msg2 = Messaging::Message->new(body => to_json($body));
$msg2->header_field("content-type", "message/error");
$msg2->text(1);
A receiver of such a message can easily decode it:
use JSON qw(from_json);
# get a message from somewhere...
$msg2 = ...;
# extract the body which is a JSON object
$body = from_json($msg2->body());
# extract the inner message
$msg1 = Messaging::Message->dejsonify($body->{message});
π STRINGIFICATION AND SERIALIZATION π
In addition to the JSON mapping described above, we also define how to stringify and serialize a message.
A stringified message is the string representing its equivalent JSON object. A stringified message is a text string and can for instance be used in another message. See the stringify() and destringify() methods.
A serialized message is the UTF-8 encoding of its stringified representation. A serialized message is a binary string and can for instance be stored in a file. See the serialize() and deserialize() methods.
For instance, here are the steps needed in order to store a message into a file:
- transform the programming language specific abstraction of the message into a JSON object
- transform the JSON object into its (text) string representing
- transform the JSON text string into a binary string using UTF-8 encoding
"1" is called jsonify, "1 + 2" is called stringify and "1 + 2 + 3" is called serialize.
To sum up:
Messaging::Message object
| ^
jsonify() | | dejsonify()
v |
JSON compatible hash reference
| ^
JSON encode | | JSON decode
v |
text string
| ^
UTF-8 encode | | UTF-8 decode
v |
binary string
π METHODS π
The following methods are available, bestie:
new([OPTIONS])β return a newMessaging::Messageobject (class method)dejsonify(HASHREF)β return a newMessaging::Messageobject from a compatible JSON object (class method)destringify(STRING)β return a newMessaging::Messageobject from its stringified representation (class method)deserialize(STRING)β return a newMessaging::Messageobject from its serialized representation (class method)jsonify([OPTIONS])β return the JSON object (a hash reference) representing the messagestringify([OPTIONS])β return the text string representation of the messageserialize([OPTIONS])β return the binary string representation of the messagebody([STRING])β get/set the body attribute, which is a text or binary stringheader([HASHREF])β get/set the header attribute, which is a hash reference (note: the hash reference is used directly, without any deep copy)header_field(NAME[, VALUE])β get/set the given header field, identified by its nametext([BOOLEAN])β get/set the text attribute, which is a boolean indicating whether the message body is a text string or not, the default is false (so binary body)size()β get the approximate message size, which is the sum of the sizes of its components: header key/value pairs and body, plus framingcopy()β return a new message which is a copy of the given one, with deep copy of the header and body
The jsonify(), stringify() and serialize() methods can be given options.
Currently, the only supported option is "compression" and it can contain either an algorithm name like "zlib" (meaning: use this algorithm only if the compressed body is indeed smaller) or an algorithm name followed by an exclamation mark to always force compression.
Here is for instance how to serialize a message, with forced compression:
$bytes = $msg->serialize(compression => "zlib!");
In addition, in order to avoid string copies, the following methods are also available:
body_ref([STRINGREF])stringify_ref([OPTIONS])destringify_ref(STRINGREF)serialize_ref([OPTIONS])deserialize_ref(STRINGREF)
They work like their counterparts but use as input or output string references instead of strings, which can be more efficient for large strings. Here is an example:
# get a copy of the body, yielding to internal string copy
$body = $msg->body();
# get a reference to the body, with no string copies
$body_ref = $msg->body_ref();
π SEE ALSO π
Compress::Snappy, Compress::LZ4, Compress::Zlib, Encode, JSON.
π AUTHOR π
Lionel Cons http://cern.ch/lionel.cons
Copyright (C) CERN 2011-2016
That's the whole page, bestie π«Ά This one is perl v5.32.1, dated 2019-07-26 β now go slay your messages πβ¨
π¦ where this page comes from
- epel 9: perl-Messaging-Message 1.6.1-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