4

I have a java backend, where I can send messages to topics via

jmsTemplate.convertAndSend("topic", "Hello World!");

In my javascript frontend I use mqttJS to connect to activeMQ and recieve the massage:

    let mqtt = require('mqtt')
    let options ={
        clientId:"test",
        username:"username",
        useSSL: true,
        password:"password",
        clean:true};
    let client  = mqtt.connect(
        'wss://someUrl.com:61619',
        options);

    client.on('connect', function () {
        client.subscribe('myTopic', function (err) {
            if (!err) {
                console.log("successfully connected to myTopic'");
            }
        })
    });

    client.on('message', function (topic, message) {
        console.log(message.toString());
    });

The message I recieve from the backend is something like this:

S�A S�)�x-opt-jms-destQ�x-opt-jms-msg-typeQ Ss�   f    
�/ID:myID@�topic://myTopic@@@@�  j��< St�
e Sw�  Hello World!

My message "Hello World!" is there. But also a bunch of unreadable into, I would guess from the header.

I tried different MessageConverters on the backend side and different parsers on the frontend side. Nothing works.

What do I need to do, to get just "Hello World!" as message? Or is there a better way to send the message, using jms, which is required.

Chris
  • 5,109
  • 3
  • 19
  • 40
  • what's your mqtt supported version? – Ori Marko May 27 '19 at 09:13
  • I am using mqtt 3.0.0 – Chris May 28 '19 at 08:35
  • Does this help? `MQTT messages are transformed into an JMS ByteMessage. Conversely, the body of any JMS Message is converted to a byte buffer to be the payload of an MQTT message.` from http://activemq.apache.org/mqtt – Ben T Jun 01 '19 at 00:18
  • No because the message in the ``` client.on() ``` is exactly this byte buffer, but it already contains the header. – Chris Jun 03 '19 at 13:27

2 Answers2

0

If tou are using mqtt 3.0.0 you should add extra parameters:

If you are connecting to a broker that supports only MQTT 3.1 (not 3.1.1 compliant), you should pass these additional options:

{
   protocolId: 'MQIsdp',
   protocolVersion: 3
 }
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • @theshadog can you upgrade it? because there might have issues, from link: *Another breaking change is that MQTT.js now defaults to MQTT v3.1.1, so to support old brokers, please read the* [client options doc](https://www.npmjs.com/package/mqtt#client) – Ori Marko May 28 '19 at 10:21
  • It's an AmazonMQ running a 5.15.6 engine. Have to talk to our DevOps guy, if/how we can upgrade to 5.16.9, which seams to support 3.1. I just introduced MQTT.js to get notifications of one topic in the frontend. If there is a better way to recieve those messages, I could easily switch. – Chris May 28 '19 at 10:28
0

Update:

After switching to ActiveMQ Artemis, with supports JMS 2, it works perfectly fine without any regex.

Old post:

Since I didn't find any solution to filter the message's body or to send the message without a header (related, unanswered question is here: How to send plain text JmsMessage with empty header) the solution was to send an JSON object and filter on the JSON syntax in the frontend with a regex.

Backend:

private void sendHelloWorld() {
    Map<String, String> subPayload = new HashMap<>();
    subPayload.put("test1", "value2");
    subPayload.put("test2", "value3");
    Map<String, Object> payload = new HashMap<>();
    payload.put("message", "Hello World!");
    payload.put("context", "Something");
    payload.put("map", subPayload);
    jmsTemplate.setMessageConverter(new MappingJackson2MessageConverter());
    jmsTemplate.convertAndSend( "notification/prediction", payload );
}

Frontend:

client.on('message', function (topic, message, packet) {
    const regex = /\{(.*)\}$/g;
    const match = message.toString().match(regex);
    if(null === match) {
        console.error("Could not parse message");
        console.log('message', message.toString());
    }
    const json = JSON.parse(match[0]);
    console.log('message', json);
});

The result would be:

{
  "message":"Hello World!", 
  "context":"Something",
  "map": {
     "test1":"value2",
     "test2":"value3"
   }
}
Chris
  • 5,109
  • 3
  • 19
  • 40