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.
If tou are using mqtt 3.0.0 you should add extra parameters:
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:
Frontend:
The result would be: