mqttr
TypeScript icon, indicating that this package has built-in type declarations

3.1.0 • Public • Published

mqttr

NPM version Build Status Dependency Status Coverage percentage

A routable mqtt library based on mqtt.js

Installation

$ npm i mqttr

Usage

import {connect, Message} from 'mqttr';

// eslint-disable-next-line  @typescript-eslint/no-floating-promises
(async () => {
  // You should start a mqtt server at 1883 before or after run this script
  const client = connect('mqtt://localhost');

  client.on('connect', function () {
    console.log('connect');
  });

  client.on('reconnect', function () {
    console.log('reconnect');
  });

  client.on('close', function () {
    console.log('close');
  });

  client.on('offline', function () {
    console.log('offline');
  });

  client.on('error', function (err: Error) {
    throw err;
  });

  // full params handler
  await client.subscribe(
    '/users/:userId/message/:messageId/:splats*',
    (topic: string, payload: any, message?: Message) => {
      message = message!;
      console.log('-------------------------------------------------');
      console.log('topic  :', topic); // => /users/yvan/message/4321/ping
      console.log('message:', payload); // => { hello: '🦄' }
      console.log('params :', message.params); // => { userId: 'yvan', messageId: 4321, splats: [ 'ping' ] }
      console.log('path   :', message.path); // => '/users/:userId/message/:messageId/:splats*'
      console.log('packet :', message.packet); // => {...} packet received packet, as defined in mqtt-packet
      console.log();
    },
  );

  // one context param handler
  await client.subscribe('/users/:userId/message/:messageId/:splats*', (message: Message) => {
    console.log('-------------------------------------------------');
    console.log(message);
    console.log();
  });

  await client.ready();

  await client.publish('/users/yvan/message/4321/ping', {hello: '🦄'});

  // eslint-disable-next-line @typescript-eslint/no-misused-promises
  setTimeout(() => client.end(true), 10);
})();

Topic Patterns

See path-to-regexp

License

MIT © taoyuan

Readme

Keywords

Package Sidebar

Install

npm i mqttr

Weekly Downloads

2

Version

3.1.0

License

MIT

Unpacked Size

472 kB

Total Files

118

Last publish

Collaborators

  • towyuan