kamilfb / mqtt-spy

Please use the new Eclipse Paho GitHub repo instead.

Home Page:https://github.com/eclipse/paho.mqtt-spy/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Interfacing With Message Object - binary payloads

ddeletic opened this issue · comments

Scripting: It would be great if there was a way of getting/setting message payloads with raw binary data rather than just strings. If the method already exists, it is not obvious.

Hi @ddeletic - I've updated the wiki with additional info. Is that what you were after? https://github.com/kamilfb/mqtt-spy/wiki/Scripting

Thanks for the quick response. It is great that the magic is already in there and I do not need to update my mqtt-spy.

One thing I'm not clear about is how to mqttspy.publish() binary data. All payloads are converted to strings in publish(), and I cannot publish a pre-built message object.

@ddeletic, what kind of binary data would you like to send? Have you got it already in something?

function getData(fName)
{
    var Paths = Java.type("java.nio.file.Paths");
    var Path  = Java.type("java.nio.file.Path");

    var path  = Paths.get(fName);
    var Files = Java.type("java.nio.file.Files");
    if (Files.exists(path) == true)
    {
        var data  = Files.readAllBytes(path);
        return data;
    }
    else
    {
        var data = new Int8Array(0);
        return data;
    }
}
. . . 
var payload = getData(FILE_NAME);
mqttspy.publish(topic, payload);

It is important to note that this is all done inside a scripted subscription

If you want to publish binary payload, you need to use this method signature:

mqtt.publish(topic, binaryPayload, qos, retained);

So you need to add QoS and the retained flag.

The Files.readAllBytes returns a byte array (byte[]).

Here is my example of publishing an image: https://github.com/kamilfb/mqtt-spy/blob/development/mqtt-spy/src/test/resources/scripts/publish_image.js

The publish method takes either a byte array (byte[]) or a string for the payload as long as you pass in the QoS and the retained flag.

Ok, I understand now. Thank you

No problem - if you need anything more, let me know.