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

0.0.2 • Public • Published

mqtt-rx npm

This library has been forked with the permission of the creator from v6.8.2 of ngx-mqtt. I've very grateful for this.

Like its parent it provides a ReactiveX wrapper around the Mqtt.js library, with an identical API but for server-side use so:

  1. Without the Angular dependency
  2. Directly importing the Mqtt.js library
  3. Allowing use of all the transports provided by Mqtt.js

It uses observables and takes care of subscription handling and message routing.

To keep the codebase as similar as possible to its parent while removing the Angular dependency, I have copied the EventEmitter implementation from the Angular source.

Since I am still carrying out integration tests, it is not yet production-ready. I also haven't yet done a comprehensive audit so it's possible there are obsolete code and config files hanging around.

Usage

import { Subscription } from 'rxjs';
 
import {
  IMqttMessage,
  MqttService,
  IMqttServiceOptions
} from 'mqtt-rx';
 
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = {
  hostname: 'localhost',
  port: 1883,
  path: '/mqtt'
};
 
export class ExampleComponent {
  private subscription: Subscription;
  public message: string;
  private _mqttService: MqttService;
 
  constructor() {
    this._mqttService = new MqttService(MQTT_SERVICE_OPTIONS);
    this.subscription = this._mqttService.observe('my/topic').subscribe((message: IMqttMessage) => {
      this.message = message.payload.toString();
    });
  }
 
  public unsafePublish(topic: string, message: string): void {
    this._mqttService.unsafePublish(topic, message, {qos: 1, retain: true});
  }
 
  public destroy() {
    this.subscription.unsubscribe();
  }
}

Readme

Keywords

Package Sidebar

Install

npm i mqtt-rx

Weekly Downloads

2

Version

0.0.2

License

MIT

Unpacked Size

34.9 kB

Total Files

11

Last publish

Collaborators

  • sirockin