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

vmind_mqtt

v1.2.1

Published

The **MQTTHandler** library is a lightweight, static TypeScript-based MQTT client utility designed to simplify real-time messaging for IoT applications. It ensures a **single MQTT connection** across your application and provides robust **event-driven com

Readme

MQTTHandler Library

Overview

The MQTTHandler library is a lightweight, static TypeScript-based MQTT client utility designed to simplify real-time messaging for IoT applications. It ensures a single MQTT connection across your application and provides robust event-driven communication via EventEmitter.

Features

  • Static Design: Manages a single MQTT client instance, removing the need for multiple instantiations.
  • Secure Communication: Supports SSL/TLS certificates for encrypted MQTT connections.
  • Event-Driven: Uses EventEmitter for handling incoming messages.
  • Error Handling: Provides detailed error messages for debugging.
  • Flexible: Supports both plaintext and secure MQTT connections.

Installation

Install the package via npm:

npm install vmind_mqtt

Deprecation Notice

⚠️ initialize(mqttUrl: string) is deprecated!
Use initializeWithUrl() or initializeWithCAOptions() instead.


Usage

1. Initialize the MQTT Client

Option 1: Basic Connection (Unsecured)

MQTTHandler.initializeWithUrl("mqtt://broker.hivemq.com");
  • Uses default port 1883 for plaintext (non-TLS) connections.

Option 2: Secure Connection with CA (Recommended)

MQTTHandler.initializeWithCAOptions({
  host: "your-mqtt-broker.com",
  port: 8883,
  protocol: "mqtts", // Secure MQTT
  ca: fs.readFileSync("path/to/ca.crt").toString(),
  cert: fs.readFileSync("path/to/client.crt").toString(),
  key: fs.readFileSync("path/to/client.key").toString(),
  rejectUnauthorized: true, // Enforce strict SSL validation in production
});
  • Uses port 8883 for SSL/TLS communication.
  • Requires valid CA, client certificate, and client key.
  • Prevents MITM attacks by enforcing encryption.

2. Publish a Message

MQTTHandler.publish("test/topic", JSON.stringify({ message: "Hello, MQTT!" }));

3. Subscribe & Listen to a Topic

MQTTHandler.listen("test/topic");

MQTTHandler.onMessage((data) => {
  console.log(`Received message from ${data.topic}:`, data.message);
});

4. Stop Listening to a Topic

MQTTHandler.stopListening("test/topic");

5. Close the MQTT Connection

MQTTHandler.close();

API Reference

| Method | Description | | ------------------------------------------------------ | ------------------------------------------------------------------- | | initialize(mqttUrl: string) | (Deprecated) Initializes MQTT client with a broker URL. | | initializeWithUrl(mqttUrl: string) | Initializes MQTT using a broker URL. | | initializeWithCAOptions(options: MQTTHandlerOptions) | (Recommended) Secure MQTT connection with SSL/TLS certificates. | | publish(topic: string, message: string) | Publishes a message to an MQTT topic. | | listen(topic: string) | Subscribes to an MQTT topic. | | onMessage(callback) | Registers a callback to handle received messages. | | stopListening(topic: string) | Unsubscribes from a topic. | | close() | Gracefully closes the MQTT connection. |


Example Usage

import { MQTTHandler } from "vmind_mqtt/dist";
import fs from "fs";

// Secure connection with SSL/TLS
MQTTHandler.initializeWithCAOptions({
  host: "your-mqtt-broker.com",
  port: 8883,
  protocol: "mqtts",
  ca: fs.readFileSync("path/to/ca.crt").toString(),
  cert: fs.readFileSync("path/to/client.crt").toString(),
  key: fs.readFileSync("path/to/client.key").toString(),
  rejectUnauthorized: true, // Enforce strict SSL validation
});

// Subscribe and listen to a topic
MQTTHandler.listen("example/topic");
MQTTHandler.onMessage((data) => {
  console.log(`Received message from ${data.topic}:`, data.message);
});

// Publish a message
MQTTHandler.publish(
  "example/topic",
  JSON.stringify({ message: "Secure MQTT!" })
);

// Stop listening to the topic
MQTTHandler.stopListening("example/topic");

// Close the connection after some time
setTimeout(() => {
  MQTTHandler.close();
}, 5000);

🔹 Why Use the Static Approach?

Singleton Pattern → Ensures only one MQTT connection per application.
Simplified API → No need for manual instantiation.
Event-Driven Communication → Uses EventEmitter for efficient message handling.
Lightweight & Efficient → Works well for IoT & real-time applications.


License

This library is licensed under the MIT License. See the LICENSE file for details.

Contributing

Feel free to submit issues or pull requests to improve the library!

Support

If you encounter any issues, open a GitHub issue or reach out for support.


Emi Roberti - Happy coding