Skip to content

Formatting

Kamil Baczkowicz edited this page Mar 6, 2016 · 4 revisions

Formatting

In mqtt-spy and mqtt-spy-daemon “formatting” is a means of modifying the payload of all messages received on a connection. This can be used for supporting environments where all messages use one or combination of the following:

  • Envelope (e.g. XML-based), and you just want to extract the body of the messages
  • Encoding (e.g. HEX or Base64) or compression, where you want to see the decoded content
  • Signature, to confirm it is a valid message
  • Encryption, to automatically decrypt the payload

For basic requirements, it might be sufficient to use one of the default formatters:

  • encode to HEX
  • decode from HEX
  • encode to Base64
  • decode from Base64
  • pretty (nicely formatted) JSON
  • pretty (nicely formatted) XML

For more advanced users it is recommend to use scripted formatters. For both mqtt-spy and mqtt-spy-daemon they are stored in the XML configuration (and are Base64 encoded). To make configuring and testing them easier, you can also define them via the mqtt-spy UI (Menu → Window → Open view → Formatters):

Sample formatting script

function format()
{
    return receivedMessage.getPayload() + " - modified a bit!";
}

Please note that each formatting script requires the format function, which returns the modified payload. You can access the received message using the receivedMessage object.

An optional before function can also be defined to set-up any additional resources for all subsequent calls to the format function.

For pretty formatting in the message browser window define a pretty() function.

Formatting Eclipse Kura payload

To format Eclipse Kura messages, you can either use the built-in formatter (with pretty JSON formatting) or create a new one with the following content, giving you Eclipse Kura messages shown as JSON:

function format()
{	
	var KuraPayloadFormatter = Java.type("pl.baczkowicz.mqttspy.kura.KuraPayloadFormatter");
	return KuraPayloadFormatter.format(receivedMessage.getRawBinaryPayload());
}

Once added, in your subscription tab switch to the new formatter (Tools -> Formatting -> Custom -> your new formatter).