Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Mosca & Redis & Ascoltatori

andreareginato edited this page Dec 29, 2014 · 3 revisions

Mosca is a great tool to create an MQTT server. As you may know it's based on ascoltatori which makes it simple to create communication channels between different micro services.

When using Redis, Mosca saves the data (also JSON) as binary. This works just fine if you communicate directly with Mosca, but it can give you some headheache if you want to insert some messages directly using Ascoltatori, because Ascoltatori does not save your data as binary. You need to make it by yourself. It's not difficult. Just one line of code where you transform the data to an HEX string and a new payload where you set the message and where you tell Redis that the message is a binary one.

payload = { message: new Buffer(JSON.stringify(req.body)).toString('hex'), binary: true };
var topic = 'devices/' + req.params.id + mode;
ascoltatore.publish(topic, payload, function() {
  console.log('[API REQ] Message published to the topic', topic, payload);
});

At this point everything will work like a charm. Best!