使用MQTT.js和Mosquitto发布和订阅MQTT主题

时间:2018-12-24 11:37:26

标签: node.js mqtt mosquitto

我正在尝试使用mosquitto(在VM sudo apt-get install mosquitto内)使用node.js和此mqtt.js异步库:https://github.com/mqttjs/async-mqtt

来发布/订阅MQTT消息。

使用sudo apt-get install mosquitto-clients在本地PC上安装mosquitto CLI发布者/订阅者客户端之后,我知道它们可以正常工作,因为我可以使用以下命令成功监视发布者/订阅者会话:

  • 订户:mosquitto_sub -h ${MY_VM_IP_ADDRESS} -p 1883 -t "readings" -v -d
  • 发布者:mosquitto_pub -h ${MY_VM_IP_ADDRESS} -p 1883 -t "readings" -i foo001 -m '{"deviceId":"foo001","fooMetric":42.42}' -d

我可以看到消息从发布者到订阅者,但是当开始使用Node.js发送消息时,在订阅者CLI会话中我再也看不到消息

  • 以下javascript代码怎么了?
  • 如何使用Mqtt.js标识具有ID的发布者?
  • 订户如何通过此ID进行过滤?

我假设多个具有不同ID的发布者可以将消息发送到同一主题,而多个订阅者可以按来自同一主题的ID进行过滤。我认为这是可能的,但是下面的代码不起作用的部分原因可能是我需要妥善保管ID /主题组合?

这是我执行的mocha规范,试图发送读数

const MQTT = require("async-mqtt");

const consoleLogError = (err) => {
  if (err.response) {
    console.error(`${new Date().toISOString()} HTTP response error, ${err.response.status}: ${err.response.statusText}`);
  } else {
    console.error(`${new Date().toISOString()} No HTTP error, the stack: ${new Error(err).stack}`);
  }
};

const consoleLog = (msg) => {
  console.log(`${new Date().toISOString()} ${msg}`);
};

// {"fooMetric":42.42, "created_at":"2018-12-24T10:42:08.057Z"}
const generateReadingMsg = () => {
  const now = new Date();
  const msg = {
    "fooMetric": 42.42,
    "created_at": now.toISOString()
  };
  consoleLog(`New generated reading: ${JSON.stringify(msg)}`);
  return msg;
};

const mqttSession = {};

mqttSession.asyncInit = (hostPort, deviceId, mqttTopic) => {
  return new Promise((resolve, reject) => {
    mqttSession.mqttTopic = mqttTopic;
    mqttSession.client = MQTT.connect(`mqtts://${hostPort}`, {
      keepalive: 10,
      clientId: deviceId,
      protocolId: 'MQTT',
      clean: false,
      protocolVersion: 4,
      reconnectPeriod: 1000,
      connectTimeout: 30 * 1000,
      rejectUnauthorized: false,
    });
    return resolve();
  });
};

mqttSession._send = (msgStr) => {
  return Promise.resolve()
    .then(() => {
      return mqttSession.client.publish(mqttSession.mqttTopic, msgStr);
    })
    .then(() => {
      return mqttSession.client.end();
    })
    .catch((err) => {
      consoleLogError(err);
      throw err;
    });
}

mqttSession.asyncSend = (msgJson) => {
  const msgStr = JSON.stringify(msgJson);
  return Promise.resolve()
    .then(() => {
      mqttSession.client.on("connect", () => {
        return mqttSession._send(msgStr);
      });
    })
    .catch((err) => {
      consoleLogError(err);
      throw err;
    });
};

describe.only('MQTT readings', () => {

  // for the IP address check the VM details
  const vm_ip = "xxx.xxx.xxx.xxx";

  beforeEach(() => {
    return Promise.all([
        mqttSession.asyncInit(`${vm_ip}:1883`, "fooId", "readings")
      ]);
  });

  it('should send a reading to the MQTT broker', () => {
    console.log(`TODO run "mosquitto_sub -h ${vm_ip} -p 1883 -t "readings" -v -d"`);
    console.log(`The following MQTT-send should be equivalent to: "mosquitto_pub -h ${vm_ip} -p 1883 -t "readings" -i foo001 -m '{"deviceId":"foo001","fooMetric":42.42}' -d"`)
    return mqttSession.asyncSend(generateReadingMsg())
      .then(stuff => {
        console.log(`returned stuff from the MQTT session: ${stuff}`);
        return Promise.resolve();
      })
      .catch(error => {
        consoleLogError(error);
        throw error;
      });
  });

});

1 个答案:

答案 0 :(得分:1)

首先,您可以确定哪个客户端在MQTT * 协议级别上针对某个主题发布了给定消息。该信息只是在任何协议级别的信息中都不存在。如果需要这些信息,则需要将其包括在所发送消息的有效负载中,并在消息传递后对其进行过滤。

对于代码,您正在尝试使用mqtts://

连接到安全的MQTT代理。
mqttSession.client = MQTT.connect(`mqtts://${hostPort}`, {

除非您在VM中特别配置了Mosquitto,否则它将在端口1883上运行正常的不安全MQTT

如果删除s,则代码对我的经纪人运行正常。

mqttSession.client = MQTT.connect(`mqtt://${hostPort}`, {

* 此MQTT v3.x,具有新的MQTT v5.0规范,可以选择添加额外的元数据,但是同样,您将无法在订阅时进行过滤,只能过滤一次邮件已传递。

相关问题