Comparing version 1.4.1 to 1.8.0
{ | ||
"name": "ngx-mqtt", | ||
"version": "1.4.1", | ||
"version": "1.8.0", | ||
"description": "ngx mqtt client library", | ||
"main": "bundles/ngx-mqtt.min.js", | ||
"module": "src./index.js", | ||
"module": "./src/index.js", | ||
"typings": "./src/index.d.ts", | ||
"scripts": { | ||
"start": "http-server", | ||
"clean": "node clean", | ||
"test": "karma start", | ||
"build": "node clean && ngc && webpack", | ||
"docs": "compodoc src -p tsconfig.json", | ||
"serve:demo": "http-server", | ||
"serve:docs": "http-server documentation", | ||
"prepublish": "node clean && ngc && webpack" | ||
@@ -45,2 +48,3 @@ }, | ||
"@angular/platform-browser-dynamic": "2.4.10", | ||
"@compodoc/compodoc": "^1.0.0-beta.3", | ||
"@types/hammerjs": "2.0.33", | ||
@@ -47,0 +51,0 @@ "@types/jasmine": "2.5.35", |
@@ -1,2 +0,2 @@ | ||
# ngx-mqtt [![npm version](https://badge.fury.io/js/ngx-mqtt.svg)](https://badge.fury.io/js/ngx-mqtt) | ||
# ngx-mqtt [![npm](https://img.shields.io/npm/v/ngx-mqtt.svg)](https://www.npmjs.com/package/ngx-mqtt) [![Travis](https://img.shields.io/travis/sclausen/ngx-mqtt.svg)](https://travis-ci.org/sclausen/ngx-mqtt) | ||
@@ -12,3 +12,3 @@ This library isn't just a wrapper around MQTT.js for angular >= 2. | ||
## Description | ||
ngx-mqtt is well suited for applications with many components and many subscribers. | ||
ngx-mqtt is well suited for applications with many components and many subscribers. | ||
The problem is, if you regulary subscribe to mqtt with client libraries like `MQTT.js`, still every message is handled with an on-message-eventhandler, so you have to dispatch the received messages for yourself. | ||
@@ -28,2 +28,10 @@ So, if you have multiple components using mqtt in your code, you just want to only receive the messages for your local filter. | ||
## Run Demo Application | ||
```sh | ||
npm i # install all dependencies | ||
npm run build # build the library | ||
npm run serve:demo # start a local http server to run the demo module | ||
``` | ||
If you change something in the code of the library (anything in the `src` folder), you have to rerun `npm run build` to see the changes. Changes in `demo.module.ts` only require a page refresh. | ||
## Usage | ||
@@ -72,3 +80,3 @@ | ||
constructor(private _mqttService: MqttService) { | ||
constructor(private _mqttService: MqttService) { | ||
this._mqttService.observe('my/topic').subscribe((message: MqttMessage) => { | ||
@@ -87,5 +95,8 @@ this.myMessage = message.payload.toString(); | ||
For further usage use this module, see `demo.module.ts` and `index.html`. | ||
To see the demo in action use | ||
## Documentation | ||
```sh | ||
npm install && npm run start | ||
npm run docs # build the documentation | ||
npm run serve:docs # open a local webserver serving the documentation | ||
``` | ||
@@ -96,3 +107,3 @@ | ||
``` | ||
$ npm test | ||
npm test | ||
``` |
@@ -40,1 +40,2 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var MqttModule_1; | ||
//# sourceMappingURL=index.js.map |
@@ -9,14 +9,27 @@ /// <reference types="mqtt" /> | ||
export interface MqttServiceOptions extends ClientOptions { | ||
/** wether a new connection should be created | ||
* on creating an instance of the service */ | ||
connectOnCreate?: boolean; | ||
/** the hostname of the mqtt broker */ | ||
hostname?: string; | ||
/** the port to connect with websocket to the broker */ | ||
port?: number; | ||
/** which protocol should be used. ws and wss are supported */ | ||
protocol?: string; | ||
/** the path parameters to connect to e.g. `/mqtt` */ | ||
path?: string; | ||
} | ||
export interface MqttMessage extends Packet { | ||
/** one of the many mqtt commands | ||
* see [mqtt-packet]{@link https://github.com/mqttjs/mqtt-packet} for more information */ | ||
cmd: string; | ||
/** the mqtt topic to which this message was published to */ | ||
topic: string; | ||
/** the payload */ | ||
payload: Uint8Array; | ||
/** the quality of service */ | ||
qos: number; | ||
/** if this message is a retained message */ | ||
retain: boolean; | ||
/** if this message is a dublicate */ | ||
dup: boolean; | ||
@@ -23,0 +36,0 @@ } |
@@ -7,1 +7,2 @@ export var MqttConnectionState; | ||
})(MqttConnectionState || (MqttConnectionState = {})); | ||
//# sourceMappingURL=mqtt.model.js.map |
@@ -10,9 +10,17 @@ /// <reference types="mqtt" /> | ||
import { MqttConnectionState, MqttMessage, MqttServiceOptions, OnConnectEvent, OnErrorEvent, OnMessageEvent, PublishOptions } from './mqtt.model'; | ||
/** | ||
* With an instance of MqttService, you can observe and subscribe to MQTT in multiple places, e.g. in different components, | ||
* to only subscribe to the broker once per MQTT filter. | ||
* It also handles proper unsubscription from the broker, if the last observable with a filter is closed. | ||
*/ | ||
export declare class MqttService { | ||
private options; | ||
private client; | ||
/** a map of all mqtt observables by filter */ | ||
observables: { | ||
[filter: string]: Observable<MqttMessage>; | ||
}; | ||
/** the connection state */ | ||
state: BehaviorSubject<MqttConnectionState>; | ||
/** an observable of the last mqtt message */ | ||
messages: Subject<MQTT.Packet>; | ||
@@ -24,9 +32,24 @@ private clientId; | ||
private url; | ||
_onConnect: EventEmitter<OnConnectEvent>; | ||
_onClose: EventEmitter<void>; | ||
_onError: EventEmitter<OnErrorEvent>; | ||
_onReconnect: EventEmitter<void>; | ||
_onMessage: EventEmitter<OnMessageEvent>; | ||
private _onConnect; | ||
private _onClose; | ||
private _onError; | ||
private _onReconnect; | ||
private _onMessage; | ||
/** | ||
* The constructor needs [connection options]{@link MqttServiceOptions} regarding the broker and some | ||
* options to configure behavior of this service, like if the connection to the broker | ||
* should be established on creation of this service or not. | ||
* @param options connection and creation options for MQTT.js and this service | ||
* @param client a already created MQTT.Client | ||
*/ | ||
constructor(options: MqttServiceOptions, client?: MQTT.Client); | ||
/** | ||
* connect manually connects to the mqtt broker. | ||
* @param opts the connection options | ||
* @param client an optional MQTT.Client | ||
*/ | ||
connect(opts?: MqttServiceOptions, client?: MQTT.Client): void; | ||
/** disconnect disconnects from the mqtt client. | ||
* This method `should` be executed when leaving the application. | ||
*/ | ||
disconnect(): void; | ||
@@ -70,47 +93,18 @@ /** | ||
static filterMatchesTopic(filter: string, topic: string): boolean; | ||
private handleOnClose; | ||
private handleOnConnect; | ||
private handleOnReconnect; | ||
private handleOnError; | ||
private handleOnMessage; | ||
/** | ||
* An EventEmitter to listen to close messages | ||
* onClose.subscribe(() => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<void>} | ||
*/ | ||
/** An EventEmitter to listen to close messages */ | ||
readonly onClose: EventEmitter<void>; | ||
/** | ||
* An EventEmitter to listen to connect messages | ||
* onConnect.subscribe((message: MqttMessage) => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<OnConnectEvent>} | ||
*/ | ||
/** An EventEmitter to listen to connect messages */ | ||
readonly onConnect: EventEmitter<OnConnectEvent>; | ||
/** | ||
* An EventEmitter to listen to reconnect messages | ||
* onReconnect.subscribe(() => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<void>} | ||
*/ | ||
/** An EventEmitter to listen to reconnect messages */ | ||
readonly onReconnect: EventEmitter<void>; | ||
/** | ||
* An EventEmitter to listen to message events | ||
* onMessage.subscribe((e: OnMessageEvent) => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<OnMessageEvent>} | ||
*/ | ||
/** An EventEmitter to listen to message events */ | ||
readonly onMessage: EventEmitter<OnMessageEvent>; | ||
/** | ||
* An EventEmitter to listen to error events | ||
* onError.subscribe((e: OnErrorEvent) => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<OnErrorEvent>} | ||
*/ | ||
/** An EventEmitter to listen to error events */ | ||
readonly onError: EventEmitter<OnErrorEvent>; | ||
private _handleOnClose; | ||
private _handleOnConnect; | ||
private _handleOnReconnect; | ||
private _handleOnError; | ||
private _handleOnMessage; | ||
private _generateClientId(); | ||
} |
@@ -21,3 +21,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { MqttConnectionState } from './mqtt.model'; | ||
/** | ||
* With an instance of MqttService, you can observe and subscribe to MQTT in multiple places, e.g. in different components, | ||
* to only subscribe to the broker once per MQTT filter. | ||
* It also handles proper unsubscription from the broker, if the last observable with a filter is closed. | ||
*/ | ||
var MqttService = MqttService_1 = (function () { | ||
/** | ||
* The constructor needs [connection options]{@link MqttServiceOptions} regarding the broker and some | ||
* options to configure behavior of this service, like if the connection to the broker | ||
* should be established on creation of this service or not. | ||
* @param options connection and creation options for MQTT.js and this service | ||
* @param client a already created MQTT.Client | ||
*/ | ||
function MqttService(options, client) { | ||
@@ -27,6 +39,9 @@ var _this = this; | ||
this.client = client; | ||
/** a map of all mqtt observables by filter */ | ||
this.observables = {}; | ||
/** the connection state */ | ||
this.state = new BehaviorSubject(MqttConnectionState.CLOSED); | ||
/** an observable of the last mqtt message */ | ||
this.messages = new Subject(); | ||
this.clientId = 'client-' + Math.random().toString(36).substr(2, 19); | ||
this.clientId = this._generateClientId(); | ||
this.keepalive = 10; | ||
@@ -40,19 +55,25 @@ this.connectTimeout = 10000; | ||
this._onMessage = new EventEmitter(); | ||
this.handleOnClose = function () { | ||
this._handleOnClose = function () { | ||
_this.state.next(MqttConnectionState.CLOSED); | ||
_this._onClose.emit(); | ||
}; | ||
this.handleOnConnect = function (e) { | ||
this._handleOnConnect = function (e) { | ||
Object.keys(_this.observables).forEach(function (filter) { | ||
_this.client.subscribe(filter); | ||
}); | ||
_this.state.next(MqttConnectionState.CONNECTED); | ||
_this._onConnect.emit(e); | ||
}; | ||
this.handleOnReconnect = function () { | ||
this._handleOnReconnect = function () { | ||
Object.keys(_this.observables).forEach(function (filter) { | ||
_this.client.subscribe(filter); | ||
}); | ||
_this.state.next(MqttConnectionState.CONNECTING); | ||
_this._onReconnect.emit(); | ||
}; | ||
this.handleOnError = function (e) { | ||
this._handleOnError = function (e) { | ||
_this._onError.emit(e); | ||
console.error(e); | ||
}; | ||
this.handleOnMessage = function (topic, msg, packet) { | ||
this._handleOnMessage = function (topic, msg, packet) { | ||
_this._onMessage.emit(packet); | ||
@@ -68,2 +89,7 @@ if (packet.cmd === 'publish') { | ||
} | ||
/** | ||
* connect manually connects to the mqtt broker. | ||
* @param opts the connection options | ||
* @param client an optional MQTT.Client | ||
*/ | ||
MqttService.prototype.connect = function (opts, client) { | ||
@@ -88,8 +114,11 @@ var options = extend(this.options || {}, opts); | ||
} | ||
this.client.on('connect', this.handleOnConnect); | ||
this.client.on('close', this.handleOnClose); | ||
this.client.on('error', this.handleOnError); | ||
this.client.on('reconnect', this.handleOnReconnect); | ||
this.client.on('message', this.handleOnMessage); | ||
this.client.on('connect', this._handleOnConnect); | ||
this.client.on('close', this._handleOnClose); | ||
this.client.on('error', this._handleOnError); | ||
this.client.on('reconnect', this._handleOnReconnect); | ||
this.client.on('message', this._handleOnMessage); | ||
}; | ||
/** disconnect disconnects from the mqtt client. | ||
* This method `should` be executed when leaving the application. | ||
*/ | ||
MqttService.prototype.disconnect = function () { | ||
@@ -217,9 +246,3 @@ if (!this.client) { | ||
Object.defineProperty(MqttService.prototype, "onClose", { | ||
/** | ||
* An EventEmitter to listen to close messages | ||
* onClose.subscribe(() => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<void>} | ||
*/ | ||
/** An EventEmitter to listen to close messages */ | ||
get: function () { | ||
@@ -232,9 +255,3 @@ return this._onClose; | ||
Object.defineProperty(MqttService.prototype, "onConnect", { | ||
/** | ||
* An EventEmitter to listen to connect messages | ||
* onConnect.subscribe((message: MqttMessage) => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<OnConnectEvent>} | ||
*/ | ||
/** An EventEmitter to listen to connect messages */ | ||
get: function () { | ||
@@ -247,9 +264,3 @@ return this._onConnect; | ||
Object.defineProperty(MqttService.prototype, "onReconnect", { | ||
/** | ||
* An EventEmitter to listen to reconnect messages | ||
* onReconnect.subscribe(() => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<void>} | ||
*/ | ||
/** An EventEmitter to listen to reconnect messages */ | ||
get: function () { | ||
@@ -262,9 +273,3 @@ return this._onReconnect; | ||
Object.defineProperty(MqttService.prototype, "onMessage", { | ||
/** | ||
* An EventEmitter to listen to message events | ||
* onMessage.subscribe((e: OnMessageEvent) => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<OnMessageEvent>} | ||
*/ | ||
/** An EventEmitter to listen to message events */ | ||
get: function () { | ||
@@ -277,9 +282,3 @@ return this._onMessage; | ||
Object.defineProperty(MqttService.prototype, "onError", { | ||
/** | ||
* An EventEmitter to listen to error events | ||
* onError.subscribe((e: OnErrorEvent) => { | ||
* // do something | ||
* }); | ||
* @type {EventEmitter<OnErrorEvent>} | ||
*/ | ||
/** An EventEmitter to listen to error events */ | ||
get: function () { | ||
@@ -291,2 +290,5 @@ return this._onError; | ||
}); | ||
MqttService.prototype._generateClientId = function () { | ||
return 'client-' + Math.random().toString(36).substr(2, 19); | ||
}; | ||
return MqttService; | ||
@@ -300,1 +302,2 @@ }()); | ||
var MqttService_1; | ||
//# sourceMappingURL=mqtt.service.js.map |
@@ -1,1 +0,1 @@ | ||
[{"__symbolic":"module","version":3,"metadata":{"MqttService":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./mqtt.model","name":"MqttServiceOptions"},{"__symbolic":"reference","module":"mqtt","name":"Client"}]}],"connect":[{"__symbolic":"method"}],"disconnect":[{"__symbolic":"method"}],"observe":[{"__symbolic":"method"}],"publish":[{"__symbolic":"method"}],"unsafePublish":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"MqttService":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./mqtt.model","name":"MqttServiceOptions"},{"__symbolic":"reference","module":"mqtt","name":"Client"}]}],"connect":[{"__symbolic":"method"}],"disconnect":[{"__symbolic":"method"}],"observe":[{"__symbolic":"method"}],"publish":[{"__symbolic":"method"}],"unsafePublish":[{"__symbolic":"method"}]}}}}] | ||
[{"__symbolic":"module","version":3,"metadata":{"MqttService":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./mqtt.model","name":"MqttServiceOptions"},{"__symbolic":"reference","module":"mqtt","name":"Client"}]}],"connect":[{"__symbolic":"method"}],"disconnect":[{"__symbolic":"method"}],"observe":[{"__symbolic":"method"}],"publish":[{"__symbolic":"method"}],"unsafePublish":[{"__symbolic":"method"}],"_generateClientId":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"MqttService":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./mqtt.model","name":"MqttServiceOptions"},{"__symbolic":"reference","module":"mqtt","name":"Client"}]}],"connect":[{"__symbolic":"method"}],"disconnect":[{"__symbolic":"method"}],"observe":[{"__symbolic":"method"}],"publish":[{"__symbolic":"method"}],"unsafePublish":[{"__symbolic":"method"}],"_generateClientId":[{"__symbolic":"method"}]}}}}] |
Sorry, the diff of this file is too big to display
163645
19
547
105
36
1559