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

@m-reiniger/node-mqtt

v1.0.2

Published

All in one nodeJS MQTT implementation

Downloads

9

Readme

Basic MQTT Broker and Client Implementation

This project is a Proof of Concept of a basic MQTT broker and client implementation in NodeJS/Typescript. MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol commonly used in IoT (Internet of Things) applications.

I am using Aedes as a messaging broker and MQTT.js as client.

Features

This implementation supports:

  • all in one solution
  • insecure connections
  • username/password authentication
  • TLS connections
  • Log Level Based Logging
  • customizable event handlers
  • client can act as both, subscriber and publisher

Install Package

npm i --save @m-reiniger/node-mqtt

Example Usage Standalone

Install Dependencies:

npm install

Build:

npm run build

Run Broker:

npm run broker

Rum Subscriber Client:

npm run subscriber

Run Publisher Client:

npm run publisher

How to Use

Broker

create options object

const options = {
    port: 8883,
    auth: {
        useAuth: true,
        username: 'user',
        password: 'pass'
    },
    tls: {
        useTLS: true,
        key: '--- your key file contents here ---',
        cert: '--- your cert file contents here ---'
    }
}

create and start broker

import { MQTTBroker } from "@m-reiniger/node-mqtt";

// create broker
const broker = new MQTTBroker(options);
// start broker server
broker.start();

Client

create options object

const options = {
    port: 8883,
    host: 'localhost'
    auth: {
        useAuth: true,
        username: 'user',
        password: 'pass'
    },
    tls: {
        useTLS: true,
        key: '--- your key file contents here ---',
        cert: '--- your cert file contents here ---'
    }
}

create client and connect

import { MQTTClient } from "@m-reiniger/node-mqtt";

// create client
const client = new MQTTClient(options);
// connect to broker
client.connect();

subscribe to a topic

client.subscribe('my/topic', (topic: string, message: Buffer) => {
    console.log(`received "${message.toString()}" on topic ${topic}`);
    // your code here
});

publish a message

client.publish('my/topic', 'my message');

Examples

See an example usage implementation in src/example.ts.

API

See API Docs.

TLS

In order to use TLS you need to create key and cert files first. You can use the provided cert/*.pem files for testing purposes, BUT I HIGHLY RECOMMEND to create your own.

To create a TLS/OpenSSL certificate for secure communication, you can follow the instructions provided in the link: How to create TLS/OpenSSL Cert. This guide will walk you through the process of setting up a certificate for local HTTPS development.