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

mini-mqtt-bus

v1.0.0

Published

MQTT client wrapper with event bus for WeChat Mini Programs

Downloads

6

Readme

📦 mini-mqtt-bus

✅ 一款专为微信小程序设计的 MQTT + 事件总线组合模块,基于 mqtt.js 封装,支持自动重连、主题订阅分发等功能。


🚀 安装

# 使用 npm
npm install mini-mqtt-bus

# 使用 pnpm / yarn 也可
pnpm add mini-mqtt-bus

📦 目录结构建议(建议这样组织)

your-miniapp/
├── utils/
│   ├── mqttClient.ts      # 引入并使用 mini-mqtt-bus
│   └── eventBus.ts        # 可选:你也可以使用自己的事件总线

🧱 模块功能

  • ✅ MQTT 连接/断开/重连监听
  • ✅ 支持动态传入连接参数(灵活复用)
  • ✅ 支持按主题分发消息事件
  • ✅ 自动跳过模拟器连接
  • ✅ 提供 subscribe / publish / unsubscribe / disconnect 等接口
  • ✅ 内部使用 wx.connectSocket 实现小程序环境 MQTT 支持

✨ 快速使用示例(微信小程序)

app.ts 中初始化

import { mqttClient } from "mini-mqtt-bus";
import eventBus from "mini-mqtt-bus/eventBus"; // 内置事件总线,也可以用自己的

mqttClient.init({
  protocol: "wxs",
  host: "your-mqtt-host.com",
  port: 8084,
  path: "/mqtt",
  clientId: "wechat_" + Math.random().toString(16).substr(2, 8),
  username: "your-username",
  password: "your-password",
  keepalive: 60,
  clean: true,
  reconnectPeriod: 2000,
});

// 连接成功后订阅主题
eventBus.on("mqtt:connected", () => {
  mqttClient.subscribe("1a");
});

// 订阅成功后发送消息
eventBus.on("mqtt:subscribed:1a", () => {
  mqttClient.publish("1a", "小程序发送的一条测试消息");
});

// 接收消息
eventBus.on("mqtt:message:1a", (payload) => {
  console.log("收到消息:", payload);
});

📡 事件列表

| 事件名 | 说明 | |--------|------| | mqtt:connected | 成功建立 MQTT 连接 | | mqtt:message:<topic> | 收到某个主题的消息 | | mqtt:message | 收到任意消息,包含 { topic, payload } | | mqtt:subscribed:<topic> | 成功订阅某个主题 | | mqtt:unsubscribed:<topic> | 成功取消订阅某个主题 | | mqtt:reconnecting | 正在尝试重连 | | mqtt:closed | 连接关闭 | | mqtt:error | 连接或操作异常 | | mqtt:disconnected | 主动断开连接 |


🧩 API 文档

mqttClient.init(options: MqttOptions)

初始化连接,接收 mqtt.js 支持的连接参数对象(详见 mqtt.js connect options)。

mqttClient.subscribe(topic: string)

订阅主题,支持监听 mqtt:subscribed:<topic> 事件。

mqttClient.publish(topic: string, payload: string)

发布消息到指定主题。

mqttClient.unsubscribe(topic: string)

取消订阅指定主题。

mqttClient.disconnect()

主动断开连接。


🛡️ 注意事项

  • 模块依赖 mqtt.min.js(请确保使用兼容微信小程序的版本)
  • 若自定义事件总线,可自行替换 eventBus.ts
  • 需在小程序配置中声明相关域名和安全连接权限

📃 License

MIT © Kaze