npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mm_mqtt

v1.1.0

Published

这是超级美眉MQTT模块,适用于物联网应用开发、游戏开发

Downloads

38

Readme

mm_mqtt

超级美眉MQTT协议通讯模块,适用于物联网应用开发、游戏开发。基于mqtt.js,提供更简单易用的接口和更丰富的功能。

English Documentation

安装

npm install mm_mqtt

基本使用

初始化配置

const {
    Mqtt
} = require('mm_mqtt');

const mqtt = new Mqtt({
    host: "127.0.0.1",      // MQTT服务器地址
    port: "1883",          // MQTT服务器端口
    protocol: "mqtt",      // 协议类型:mqtt, ws(websocket)
    clientId: "test123",   // 客户端ID
    username: "admin",     // 用户名
    password: "asd123",    // 密码
    clean: false           // 清除会话
});

连接服务器

// 连接到MQTT服务器
await mqtt.run();

消息订阅

1. 基本订阅
// 订阅指定主题,并设置消息处理函数
mqtt.subscribe('test1', (msg) => {
    console.log('收到消息:', msg);
});
2. 方法订阅(RPC模式)
// 订阅主题
mqtt.subscribe('client/test');

// 注册处理方法
mqtt.methods.setting = function(param) {
    console.log("收到参数:", param);
    return {
        hi: "hello world!"
    }
};

// 支持多级方法注册
mqtt.methods.dir = {};
mqtt.methods.dir.try = function(param) {
    console.log("收到try方法调用:", param);
    return {
        hi: "try again!"
    }
};

消息发布

1. 发送普通消息
// 发送字符串消息
mqtt.send("test1", "hello world");

// 发送JSON格式消息
mqtt.send("test1", {
    id: "168667",
    method: "示例",
    params: { pm1: "参数1", pm2: "参数2" }
});
2. RPC请求
// 发送RPC请求,不需要响应
mqtt.req('client/test', "setting", {
    body: {
        hello: "world"
    }
});

// 发送RPC请求,带回调函数处理响应
mqtt.req('client/test', "setting", {
    body: {
        hello: "world"
    }
}, function(response) {
    console.log("收到响应:", response);
});

// 发送异步RPC请求(Promise方式)
async function makeRequest() {
    try {
        const result = await mqtt.reqAsync('client/test', "setting", {
            body: {
                hello: "world"
            }
        });
        console.log("收到响应:", result);
    } catch (error) {
        console.error("请求超时或失败:", error);
    }
}

其他功能

1. 断开连接
mqtt.end();
2. 取消订阅
// 取消特定主题的订阅
mqtt.unsubscribe('test1');

// 取消特定主题下的指定处理函数
const key = mqtt.subscribe('test1', callback);
mqtt.unsubscribe('test1', key);
3. 事件监听
// 监听连接错误
mqtt.on("error", (error) => {
    console.error("连接失败:", error);
});

// 监听重连事件
mqtt.on("reconnect", () => {
    console.log("正在重新连接...");
});

高级特性

1. 消息队列

模块内部维护消息队列用于RPC请求的响应匹配。每个RPC请求生成唯一ID,收到响应时自动匹配回调函数。

2. 自动重连

连接断开时自动尝试重连,最多重试5次。超过重试次数后重新初始化连接。

3. QoS支持

默认使用QoS 1级别进行消息发布和订阅,确保消息至少送达一次。

注意事项

  1. 建议先调用run()方法建立连接
  2. RPC请求默认超时时间为3秒
  3. 使用reqAsync方法时注意捕获超时异常
  4. 建议为每个客户端设置唯一clientId

许可证

ISC License