@jsh/mqtt
Client
The MQTT client.
Usage example
| |
Creation
| Constructor | Description |
|---|---|
| new Client(options) | Instantiates a MQTT client object with an options |
Options
| Option | Type | Default | Description |
|---|---|---|---|
| serverUrls | String[] | server addresses | |
| keepAlive | Number | 10 | |
| cleanStart | Boolean | true | clean session |
| username | String | ||
| password | String | ||
| clientID | String | random id | |
| debug | Boolean | false | |
| sessionExpiryInterval | Number | 60 | |
| connectRetryDelay | Number | 10 | |
| connectTimeout | Number | 10 | |
| packetTimeout | Number | 5 | |
| queue | String | memory |
connect()
Syntax
connect(opts)Parameters
optsObject
| Property | Type | Description |
|---|---|---|
| timeout | Number | connection timeout in milliseconds |
Return value
None.
disconnect()
Syntax
disconnect(opts)Parameters
optsObject
| Property | Type | Description |
|---|---|---|
| waitForEmptyQueue | Boolean | |
| timeout | Number | disconnect wait timeout in milliseconds |
Return value
None.
subscribe()
Syntax
subscribe(opts)Parameters
optsObjectSubscriptionOption
SubscriptionOption
| Property | Type | Description |
|---|---|---|
| subscriptions | Object[] | Array of Subscription |
| properties | Object | Properties |
Subscription
| Property | Type | Description |
|---|---|---|
| topic | String | |
| qos | Number | 0, 1, 2 |
| retainHandling | Number | |
| noLocal | Boolean | |
| retainAsPublished | Boolean |
Properties
| Property | Type | Description |
|---|---|---|
| user | Object | key-value properties |
Return value
None.
Usage example
const topicName = 'sensor/temperature';
client.subscribe({subscriptions:[{topic:topicName, qos:0}]});unsubscribe()
Syntax
unsubscribe(opts)Parameters
optsObjectUnsubscribeOption
UnsubscribeOption
| Property | Type | Description |
|---|---|---|
| topics | String[] | Array of topics to unsubscribe |
| properties | Object | Properties |
Properties
| Property | Type | Description |
|---|---|---|
| user | Object | user key-value properties |
Return value
None.
Usage example
const topicName = 'sensor/temperature';
client.unsubscribe({topics:[topicName]});publish()
Syntax
publish(opts, payload)Parameters
optsObjectPublishOptionspayloadStringorNumber
PublishOptions
| Property | Type | Description |
|---|---|---|
| topic | String | |
| qos | Number | 0, 1, 2 |
| packetID | String | |
| retain | Boolean | |
| properties | Object |
Return value
Object
| Property | Type | Description |
|---|---|---|
| reasonCode | Number | |
| properties | Object |
Usage example
let r = client.publish('sensor/temperature', 'Hello World', 1)
console.log(r.reasonCode)onMessage callback
Callback function that receives a message.
Syntax
function (msg) { }msgObjectMessage
Message
| Property | Type | Description |
|---|---|---|
| packetID | Number | |
| topic | String | |
| qos | Number | 0, 1, 2 |
| retain | Boolean | |
| payload | Object | Payload |
| properties | Object | Properties |
Payload
msg.payload.bytes()msg.payload.string()
Properties
| Property | Type | Description |
|---|---|---|
| correlationData | byte[] | |
| contentType | String | |
| responseTopic | String | |
| payloadFormat | Number | or undefined |
| messageExpiry | Number | or undefined |
| subscriptionIdentifier | Number | or undefined |
| topicAlias | Number | or undefined |
| user | Object | user properties |
onConnect callback
On connect callback.
Syntax
function (ack) { }Parameters
ackObject
| Property | Type | Description |
|---|---|---|
| sessionPresent | Boolean | |
| reasonCode | Number | |
| properties | Object | Properties |
Properties
| Property | Type | Description |
|---|---|---|
| reasonString | String | |
| reasonInfo | String | |
| assignedClientID | String | |
| authMethod | String | |
| serverKeepAlive | Number | or undefined |
| sessionExpiryInterval | Number | or undefined |
| user | Object |
Return value
None.
onConnectError callback
On connect error callback.
Syntax
function (err) { }Parameters
errorString
Return value
None.
onDisconnect callback
On disconnect callback
Syntax
function (disconn) { }Parameters
disconnObject
Return value
None.
onClientError callback
On client error callback
Syntax
function (err) { }Parameters
errString
Return value
None.
Last updated on