@diyaner/egg-mqtt
v3.0.4
Published
egg plugin for mqtt use typescript.
Downloads
39
Maintainers
Readme
egg-mqtt
Install
$ npm i @diyaner/egg-mqtt --save
Usage
// {app_root}/config/plugin.js
exports.mqtt = {
enable: true,
package: "@diyaner/egg-mqtt",
};
Configuration
// {app_root}/config/config.default.js
exports.mqtt = {
enable: true,
clients: [],
};
see config/config.default.js for more detail.
Example
在 app 上挂载 mqtt 客户端调用,发布订阅和接收消息。 在 agent 上挂载 mqtts 存储所有连接实例。
// 在app.ts中使用
// 推荐注册回调,当agent上mqtt连接完成后会触发回调来执行初始化逻辑
this.app.mqtt.onConnected(() => {
const client = this.app.mqtt.get(this.app, "eggMQTTPluginTest");
client.onMessage((topic, msg) => {
console.log("topic", topic);
console.log("msg", msg);
});
const opt: mqtt.IClientPublishOptions = {
retain: false,
};
client.publish(
"topic/abc/bcd",
{ a: 1, b: 2 },
{
...opt,
}
);
client.subscribe("hello/world", { qos: 2 });
});
// 其他情况,在service中调用发布,按正常使用即可。
// fetch api 通过payload的特殊字段messageId实现请求响应模型。要求消息消费者端必须携带返回messageId
Questions & Suggestions
Please open an issue here.