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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@wyltils/mqtt-utils

v1.0.4

Published

A utility package for MQTT operations

Readme

mqtt-utils

一个简单易用的 MQTT 客户端工具包,提供了 MQTT 连接管理和流量统计功能。

特性

  • 🚀 简单易用的 MQTT 客户端
  • 📊 内置流量统计功能
  • 🔄 自动重连机制
  • 🎯 支持消息观察者模式
  • 💾 消息历史记录
  • ⚡ 基于 WebSocket 的连接

安装

npm install @wyltils/mqtt-utils

使用方法

基础使用

import { mqttClient, trafficStats } from '@wyltils/mqtt-utils';

// 连接到 MQTT 服务器
mqttClient.connect('ws://your-mqtt-server:port/mqtt', 'username', 'password');

// 订阅主题
mqttClient.subscribe('your/topic', (message) => {
    console.log('收到消息:', message);
});

消息观察者

// 添加消息观察者
const handleMessage = (topic, message) => {
    console.log(`收到主题 ${topic} 的消息:`, message);
};

mqttClient.addMessageObserver(handleMessage);

// 批量订阅主题
const topics = [
    '/device/status',
    '/device/data',
    '/device/alarm'
];

topics.forEach(topic => {
    mqttClient.subscribe(topic);
});

流量统计

// 添加单个白名单
trafficStats.addWhitelistPrefix('192.168.');

// 批量添加白名单
trafficStats.addWhitelistPrefixes(['localhost', '127.0.0.1']);

// 获取流量统计
const stats = trafficStats.getStats();
console.log('当前流量统计:', stats);

API 文档

MQTTClient

connect(url, username, password, clientId)

连接到 MQTT 服务器

  • url: MQTT WebSocket 服务器地址
  • username: 用户名(可选)
  • password: 密码(可选)
  • clientId: 客户端 ID(可选,默认自动生成)

subscribe(topic, callback)

订阅主题

  • topic: 要订阅的主题
  • callback: 消息回调函数
  • 返回: 订阅 ID

unsubscribe(subscriptionId)

取消订阅

  • subscriptionId: 订阅 ID

addMessageObserver(observer)

添加消息观察者

  • observer: 观察者函数,接收 (topic, message) 参数

TrafficStats

addWhitelistPrefix(prefix)

添加白名单前缀

  • prefix: IP 前缀字符串
  • 返回: 是否添加成功

addWhitelistPrefixes(prefixes)

批量添加白名单前缀

  • prefixes: IP 前缀字符串数组
  • 返回: 是否有新增前缀

getStats()

获取流量统计信息

  • 返回: 包含 API 和 MQTT 流量统计的对象

浏览器支持

  • Chrome >= 61
  • Firefox >= 60
  • Safari >= 11
  • Edge >= 16

许可证

MIT