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 🙏

© 2024 – Pkg Stats / Ryan Hefner

denoflare-mqtt

v0.0.2

Published

Lightweight MQTT v5 client for Deno, Node, and the browser.

Downloads

21

Readme

denoflare-mqtt is a lightweight MQTT v5 client for Deno, Node, and the browser.

A Denoflare subproject.

Carefully packaged so that it can be used from either newer-style ESM-based or older-style CommonJS-based Node projects.

Written using Deno, so also can be used via remote importing without this NPM package at all (see the source module documentation).

Features

  • Isomorphic, use in the browser, Node, or Deno
  • No dependencies, bring your own WebSocket/Blob polyfills if necessary in your environment
  • TypeScript typings included
  • Implements MQTTv5, and only the features currently implemented by Cloudflare Pub/Sub

Documentation

See the API docs in mqtt_client.ts for now.

These are also used to generate TypeScript typings for this NPM package, so you'll get them as hover documentation in your IDE.

This package is a convenience for Node environments. It is not necessary at all in Deno or client-side via module-imports in the browser, since both support remote importing modules by url.

More information

See the source module documentation for more info.

Example usage in an ESM-based Node project

Installation:

# for WebSocket (Node still doesn't have this?)
npm install ws
npm install isomorphic-ws
# for Blob (only needed on Node v14 or below)
npm install cross-blob
# this package
npm install denoflare-mqtt

example.mjs

import { MqttClient, DISCONNECT } from 'denoflare-mqtt';
import isomorphicWs from 'isomorphic-ws';
import Blob from 'cross-blob';

globalThis.WebSocket = isomorphicWs.WebSocket;
globalThis.Blob = Blob;

const protocol = 'wss';
const hostname = 'broker.example.com';
const port = 8884;

const topic = 'my-topic';
const payload = 'hello world!';

const clientId = 'my-client-id'; // optional
const username = 'my-user-name'; // optional
const password = 'MY_PASSWORD';

const client = new MqttClient({ hostname, port, protocol });

client.onMqttMessage = message => {
    if (message.type === DISCONNECT) {
        console.log('disconnect', message.reason);
    }
};

console.log('connecting');
await client.connect({ clientId, username, password });

const { keepAlive } = client;
console.log('connected', { keepAlive });

console.log(`publishing`);
await client.publish({ topic, payload });

console.log('disconnecting');
await client.disconnect();

console.log('disconnected');

Example usage in a CommonJS-based Node project

Installation:

# for WebSocket (Node still doesn't have this?)
npm install ws
npm install isomorphic-ws
# for Blob (only needed on Node v14 or below)
npm install cross-blob
# this package
npm install denoflare-mqtt

example.js

const { MqttClient, DISCONNECT } = require('denoflare-mqtt');
const isomorphicWs = require('isomorphic-ws');
const Blob = require('cross-blob');

globalThis.WebSocket = isomorphicWs.WebSocket;
globalThis.Blob = Blob;

async function run() {
    const protocol = 'wss';
    const hostname = 'broker.example.com';
    const port = 8884;

    const topic = 'my-topic';
    const payload = 'hello world!';

    const clientId = 'my-client-id'; // optional
    const username = 'my-user-name'; // optional
    const password = 'MY_PASSWORD';

    const client = new MqttClient({ hostname, port, protocol });

    client.onMqttMessage = message => {
        if (message.type === DISCONNECT) {
            console.log('disconnect', message.reason);
        }
    };

    console.log('connecting');
    await client.connect({ clientId, username, password });

    const { keepAlive } = client;
    console.log('connected', { keepAlive });

    console.log(`publishing`);
    await client.publish({ topic, payload });

    console.log('disconnecting');
    await client.disconnect();

    console.log('disconnected');
}

run(); // no top-level await when using CommonJS