Creating a bridge between mosquitto MQTT and EMQX using NodeJS

792 views Asked by At

I have set up mosquitto with NodeJs and EMQX with the NodeJs in 2 different applications. I want to create a bridge between both my MQTT clients and send and receive values from both ends.

Tried creating a bridge using WebSocket Options in my MQTT npm package.

var client = mqtt.connect('mqtt://test.mosquitto.org', {
  clientId: 'mqttjs_6fa3a7ee3d',
  wsOptions: {
  host: '100.84.81.28',
  port: 8083,
  path: '/mqtt'
}
});  

My EMQ connection with NodeJs.

var client = mqtt.connect('ws://100.84.81.28:8083/mqtt');
1

There are 1 answers

3
hardillb On BEST ANSWER

As said in the comments, bridges are something you have to configure in the broker, not the client.

The following will not work, you have told the client library to connect to the broker using native MQTT with the mqtt:// url prefix. This means it will totally ignore the wsOptions block in settings object.

var client = mqtt.connect('mqtt://test.mosquitto.org', {
  clientId: 'mqttjs_6fa3a7ee3d',
  wsOptions: {
  host: '100.84.81.28',
  port: 8083,
  path: '/mqtt'
}
}); 

Also you can't have the client try to connect to 2 separate brokers at once (You can have 2 separate client instances).

To configure a bridge you need to look at the mosquitto.conf doc, you will need to run your own instance of mosquitto, you can not use test.mosquitto.org as you can not change it's config. The bridge needs to be made using native MQTT, not MQTT over websockets.