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

nano-mqtt

v1.2.0

Published

Nano-sized MQTT client intended but not limited to be used on Espruino

Downloads

14

Readme

npm version build status codecov

About

This package has been created with modularity in mind. You don't need to import any networking related code if you're planning on reading messages from stdout and shouting them at random strangers (or more conveniently using a radio module).

The core is written in ES5 to be able to run on very basic interpreters as long as they support Uint8Array (e.g. Espruino).

You are in charge of listening for the generated messages and sending them to an interface.

This is made easier by the included adapters:

  • espruino-tcp
  • espruino-udp

Usage

Install using npm:

$ npm install nano-mqtt

Node.js

const nanoMQTT = require('nano-mqtt');

const nano = new nanoMQTT('device-uuid');

Espruino TCP

const nanoMQTT = require('nano-mqtt/lib');
const nanoTCP = require('nano-mqtt/lib/adapter/espruino-tcp');

const nano = nanoTCP(
	new nanoMQTT('device-uuid'),
	'192.168.0.1',
	1883,
	10000, // connection timeout
	5000, // retry delay
);

Espruino UDP

Basic usage with a UDP broker

const nanoMQTT = require('nano-mqtt/lib');
const nanoUDP = require('nano-mqtt/lib/adapter/espruino-udp');

const nano = nanoUDP(
	new nanoMQTT('device-uuid'),
	'192.168.0.1',
	1883,
);

UDP mesh without broker

const nanoMQTT = require('nano-mqtt/lib');
const nanoUDP = require('nano-mqtt/lib/adapter/espruino-udp');

const nano = nanoUDP(
	new nanoMQTT('device-uuid'),
	'192.168.0.255',
	1883,
	1883,
);

API

Event: 'connected'

Emitted after receiving a CONNACK packet or setConnected was called with true.

nano.on('connected', () => {
	console.log('we are connected to broker');
});

Event: 'disconnected'

Emitted after setConnected was called with false.

nano.on('disconnected', () => {
	console.log('we have been disconnected from broker');
});

Event: 'message'

Emitted after receiving a PUBLISH packet.

  • topic <string>
  • payload <string>
nano.on('message', (topic, payload) => {
	console.log('received message', topic, payload);
});

Event: 'data'

Emitted when a packet has been generated and is ready to be sent.

  • packet <Uint8Array>
  • type <number>
nano.on('data', (packet, type) => {
	console.log('packet has been generated');
	socket.send(packet);
});

Event: 'error'

Emitted when trying to parse an unknown packet.

  • packet <Uint8Array>
nano.on('error', (packet) => {
	console.log('unknown packet', packet);
});

setConnected(isConnected)

  • isConnected <boolean>

Sets the connection status and emits the corresponding event (connected or disconnected).

connect()

Generates and emits a CONNECT packet.

disconnect()

Generates and emits a DISCONNECT packet.

publish(topic, payload, retain)

  • topic <string>
  • payload <string>
  • retain <boolean>

Generates and emits a PUBLISH packet. Retain is false by default.

subscribe(topic)

  • topic <string>

Generates and emits a SUBSCRIBE packet.

parse(data)

  • data <Uint8Array>

Parses data packet and emits the corresponding event, or error on unknown packet type. The parsed packet is returned in the format of [PACKET_INT_TYPE, ...value]