Skip to content
Sebastian Clausen edited this page Apr 18, 2018 · 5 revisions

API

MqttService(options?: IMqttServiceOptions, client?: MqttClient)

  • options is the client connection options. Defaults:
    • connectOnCreate: true
    • hostname:'localhost'
    • port:1884
    • protocol:'ws'
    • path:'/'
  • client is a already created MQTT.js client instance

IMqttServiceOptions extends IClientOptions of mqtt which has a lot of more fields.

MqttService#observe(filter: string)

With this method, you can observe messages for a mqtt topic. The observable will only emit messages matching the filter. The first one subscribing to the resulting observable executes a mqtt subscribe. The last one unsubscribing this filter executes a mqtt unsubscribe.

  • filter the mqtt filter to subscribe

MqttService#publish(topic: string, message: any, [options: IPublishOptions])

This method publishes a message for a topic with optional options. The returned observable will complete, if publishing was successfull and will throw an error, if the publication fails

  • topic the topic to publish the message to
  • message any message to publish
  • options the options to publish with
    • qos the qos level
    • retain set to true if message should be retained

MqttService#unsafePublish(topic: string, message: any, [options: IPublishOptions])

This method publishes a message for a topic with optional options. Although this method does return void, it will throw an error, if the publication fails

  • topic the topic to publish the message to
  • message any message to publish
  • options the options to publish with
    • qos the qos level
    • retain set to true if message should be retained

MqttService#filterMatchesTopic(filter: string, topic: string)

This static method shall be used to determine whether an MQTT topic matches a given filter. The matching rules are specified in the MQTT standard documenation and in the library test suite.

  • filter A filter may contain wildcards like '#' and '+'.
  • topic A topic may not contain wildcards.