Mosquitto - subscribe to one broker and push to another using C

Viewed 5668

Is it possible and if so how to archive it that one application using libmosquitto get messages from one broker and publish it to another?

Schematics

Its pretty simple to just change topic in mosquito_publish function, but set of broker takes place in

mosquitto_connect(mosq, "localhost",1883 , 60); 

Running mosquitto_connet second time e.g.

 mosquitto_connect(mosq, "mqtt.example.io",1883 , 60); 

ends up connecting to last one.

I tried to create two mosquitto structs but I dont know how to inform second one about message form subbed channel in order to get info from it, change it and push to proper broker.

4 Answers

This command line using mosquitto can do the job:

mosquitto_sub -h localhost -t '#'  | {  while [ 1 -lt 2 ] ;   do   read message ;   if [[ "$message" != "$prev_message" ]]; then  mosquitto_pub -h localhost -t "tenewtest" -m "$message" ; prev_message=$message ; fi ;    done ;  }

If you simply want to forward the messages, then there is a 'bridge' functionality in mosquitto broker. It makes it easier through configuration. It has a feature where you can specify the topics you want to forward & also authentication options. It is quite feature rich.

Related