mqtt.connect()
- * mqtt.Client()
- * mqtt.Client#publish()
- * mqtt.Client#subscribe()
- * mqtt.Client#unsubscribe()
- * mqtt.Client#end()
- * mqtt.Client#removeOutgoingMessage()
- * mqtt.Client#reconnect()
- * mqtt.Client#handleMessage()
- * mqtt.Client#connected
- * mqtt.Client#reconnecting
- * mqtt.Client#getLastMessageId()
- * mqtt.Store()
- * mqtt.Store#put()
- * mqtt.Store#del()
- * mqtt.Store#createStream()
- * mqtt.Store#close()
-
--------------------------------------------------------
+- [`mqtt.connect()`](#connect)
+- [`mqtt.connectAsync()`](#connect-async)
+- [`mqtt.Client()`](#client)
+- [`mqtt.Client#connect()`](#client-connect)
+- [`mqtt.Client#publish()`](#publish)
+- [`mqtt.Client#publishAsync()`](#publish-async)
+- [`mqtt.Client#subscribe()`](#subscribe)
+- [`mqtt.Client#subscribeAsync()`](#subscribe-async)
+- [`mqtt.Client#unsubscribe()`](#unsubscribe)
+- [`mqtt.Client#unsubscribeAsync()`](#unsubscribe-async)
+- [`mqtt.Client#end()`](#end)
+- [`mqtt.Client#endAsync()`](#end-async)
+- [`mqtt.Client#removeOutgoingMessage()`](#removeOutgoingMessage)
+- [`mqtt.Client#reconnect()`](#reconnect)
+- [`mqtt.Client#handleMessage()`](#handleMessage)
+- [`mqtt.Client#connected`](#connected)
+- [`mqtt.Client#reconnecting`](#reconnecting)
+- [`mqtt.Client#getLastMessageId()`](#getLastMessageId)
+- [`mqtt.Store()`](#store)
+- [`mqtt.Store#put()`](#put)
+- [`mqtt.Store#del()`](#del)
+- [`mqtt.Store#createStream()`](#createStream)
+- [`mqtt.Store#close()`](#close)
+
+---
+
+
### mqtt.connect([url], options)
Connects to the broker specified by the given url and options and
returns a [Client](#client).
The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp',
-'tls', 'ws', 'wss', 'wxs', 'alis'. The URL can also be an object as returned by
+'tls', 'ws', 'wss', 'wxs', 'alis'. If you are trying to connect to a unix socket just append the `+unix` suffix to the protocol (ex: `mqtt+unix`). This will set the `unixSocket` property automatically.
+
+The URL can also be an object as returned by
[`URL.parse()`](http://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost),
in that case the two objects are merged, i.e. you can pass a single
object with both the URL and the connect options.
@@ -312,89 +375,126 @@ at every connect.
For all MQTT-related options, see the [Client](#client)
constructor.
--------------------------------------------------------
+
+
+### connectAsync([url], options)
+
+Asynchronous wrapper around the [`connect`](#connect) function.
+
+Returns a `Promise` that resolves to a `mqtt.Client` instance when the client
+fires a `'connect'` or `'end'` event, or rejects with an error if the `'error'`
+is fired.
+
+Note that the `manualConnect` option will cause the promise returned by this
+function to never resolve or reject as the underlying client never fires any
+events.
+
+---
+
+
### mqtt.Client(streamBuilder, options)
The `Client` class wraps a client connection to an
MQTT broker over an arbitrary transport method (TCP, TLS,
WebSocket, ecc).
+`Client` is an [EventEmitter](https://nodejs.org/en/learn/asynchronous-work/the-nodejs-event-emitter) that has it's own [events](#events)
`Client` automatically handles the following:
-* Regular server pings
-* QoS flow
-* Automatic reconnections
-* Start publishing before being connected
+- Regular server pings
+- QoS flow
+- Automatic reconnections
+- Start publishing before being connected
The arguments are:
-* `streamBuilder` is a function that returns a subclass of the `Stream` class that supports
-the `connect` event. Typically a `net.Socket`.
-* `options` is the client connection options (see: the [connect packet](https://github.com/mcollina/mqtt-packet#connect)). Defaults:
- * `wsOptions`: is the WebSocket connection options. Default is `{}`.
- It's specific for WebSockets. For possible options have a look at: https://github.com/websockets/ws/blob/master/doc/ws.md.
- * `keepalive`: `60` seconds, set to `0` to disable
- * `reschedulePings`: reschedule ping messages after sending packets (default `true`)
- * `clientId`: `'mqttjs_' + Math.random().toString(16).substr(2, 8)`
- * `protocolId`: `'MQTT'`
- * `protocolVersion`: `4`
- * `clean`: `true`, set to false to receive QoS 1 and 2 messages while
+- `streamBuilder` is a function that returns a subclass of the `Stream` class that supports
+ the `connect` event. Typically a `net.Socket`.
+- `options` is the client connection options (see: the [connect packet](https://github.com/mcollina/mqtt-packet#connect)). Defaults:
+ - `wsOptions`: is the WebSocket connection options. Default is `{}`.
+ It's specific for WebSockets. For possible options have a look at: mqtt.Client#end()
is called.
+Emitted when [`mqtt.Client#end()`](#end) is called.
If a callback was passed to `mqtt.Client#end()`, this event is emitted once the
callback returns.
@@ -469,9 +572,10 @@ callback returns.
`function (topic, message, packet) {}`
Emitted when the client receives a publish packet
-* `topic` topic of the received packet
-* `message` payload of the received packet
-* `packet` received packet, as defined in
+
+- `topic` topic of the received packet
+- `message` payload of the received packet
+- `packet` received packet, as defined in
[mqtt-packet](https://github.com/mcollina/mqtt-packet#publish)
#### Event `'packetsend'`
@@ -480,7 +584,8 @@ Emitted when the client receives a publish packet
Emitted when the client sends any packet. This includes .published() packets
as well as packets used by MQTT for managing subscriptions and connections
-* `packet` received packet, as defined in
+
+- `packet` received packet, as defined in
[mqtt-packet](https://github.com/mcollina/mqtt-packet)
#### Event `'packetreceive'`
@@ -490,92 +595,134 @@ as well as packets used by MQTT for managing subscriptions and connections
Emitted when the client receives any packet. This includes packets from
subscribed topics as well as packets used by MQTT for managing subscriptions
and connections
-* `packet` received packet, as defined in
+
+- `packet` received packet, as defined in
[mqtt-packet](https://github.com/mcollina/mqtt-packet)
--------------------------------------------------------
+---
+
+
+
+### mqtt.Client#connect()
+
+By default client connects when constructor is called. To prevent this you can set `manualConnect` option to `true` and call `client.connect()` manually.
+
+
### mqtt.Client#publish(topic, message, [options], [callback])
Publish a message to a topic
-* `topic` is the topic to publish to, `String`
-* `message` is the message to publish, `Buffer` or `String`
-* `options` is the options to publish with, including:
- * `qos` QoS level, `Number`, default `0`
- * `retain` retain flag, `Boolean`, default `false`
- * `dup` mark as duplicate flag, `Boolean`, default `false`
- * `properties`: MQTT 5.0 properties `object`
- * `payloadFormatIndicator`: Payload is UTF-8 Encoded Character Data or not `boolean`,
- * `messageExpiryInterval`: the lifetime of the Application Message in seconds `number`,
- * `topicAlias`: value that is used to identify the Topic instead of using the Topic Name `number`,
- * `responseTopic`: String which is used as the Topic Name for a response message `string`,
- * `correlationData`: used by the sender of the Request Message to identify which request the Response Message is for when it is received `binary`,
- * `userProperties`: The User Property is allowed to appear multiple times to represent multiple name, value pairs `object`,
- * `subscriptionIdentifier`: representing the identifier of the subscription `number`,
- * `contentType`: String describing the content of the Application Message `string`
- * `cbStorePut` - `function ()`, fired when message is put into `outgoingStore` if QoS is `1` or `2`.
-* `callback` - `function (err)`, fired when the QoS handling completes,
+- `topic` is the topic to publish to, `String`
+- `message` is the message to publish, `Buffer` or `String`
+- `options` is the options to publish with, including:
+ - `qos` QoS level, `Number`, default `0`
+ - `retain` retain flag, `Boolean`, default `false`
+ - `dup` mark as duplicate flag, `Boolean`, default `false`
+ - `properties`: MQTT 5.0 properties `object`
+ - `payloadFormatIndicator`: Payload is UTF-8 Encoded Character Data or not `boolean`,
+ - `messageExpiryInterval`: the lifetime of the Application Message in seconds `number`,
+ - `topicAlias`: value that is used to identify the Topic instead of using the Topic Name `number`,
+ - `responseTopic`: String which is used as the Topic Name for a response message `string`,
+ - `correlationData`: used by the sender of the Request Message to identify which request the Response Message is for when it is received `binary`,
+ - `userProperties`: The User Property is allowed to appear multiple times to represent multiple name, value pairs `object`,
+ - `subscriptionIdentifier`: representing the identifier of the subscription `number`,
+ - `contentType`: String describing the content of the Application Message `string`
+ - `cbStorePut` - `function ()`, fired when message is put into `outgoingStore` if QoS is `1` or `2`.
+- `callback` - `function (err, packet)`, fired when the QoS handling completes,
or at the next tick if QoS 0. An error occurs if client is disconnecting.
--------------------------------------------------------
+
+
+### mqtt.Client#publishAsync(topic, message, [options])
+
+Async [`publish`](#publish). Returns a `PromiseAdam Rudd | GitHub/adamvr | Twitter/@adam_vr |
---|---|---|
Matteo Collina | GitHub/mcollina | Twitter/@matteocollina |
Maxime Agor | GitHub/4rzael | Twitter/@4rzael |
Siarhei Buntsevich | GitHub/scarry1992 |
Welcome to your Electron application.
+ Mqtt connection status: offline