The Client class provides a unified pubsub client in node.js and browsers. It supports subscribing to topics or topic patterns (topics + wildcards). Clients can connect to brokers or proxies that support the client.adapter’s protocol.
var Client = require('strong-pubsub');
var Adapter = require('strong-pubsub-mqtt');
var client = new Client({
host: 'my.mqtt-broker.com',
port: 6543
}, Adapter);
Event: connect
Emitted on successful connection (or reconnection).
Event: error
Emitted when a connection could not be established.
Name | Type | Description |
---|---|---|
options |
Object
|
See |
Adapter |
Function
|
The adapter constructor. |
[transport] |
Object
|
The optional transport module implementing |
Name | Type | Description |
---|---|---|
options |
Object
|
The options passed as the first argument to the |
options.hostname |
String
|
required The host to connect to. |
options.port |
String
|
required The port to connect to. |
adapter |
Adapter
|
|
transport |
Object
|
The transport module (eg. |
Connect to the broker or proxy.
Name | Type | Description |
---|---|---|
callback |
Function
|
Added as a |
Name | Type | Description |
---|---|---|
err |
Error
|
A connection error (if one occured). |
Disconnect from the broker or proxy.
Name | Type | Description |
---|---|---|
callback |
Function
|
Added as a |
Name | Type | Description |
---|---|---|
err |
Error
|
A connection error (if one occured). |
Publish a message
to the specified topic
.
Name | Type | Description |
---|---|---|
topic |
String
|
The topic to publish to. |
message |
String or Buffer
|
The message to publish to the topic. |
[options] |
Object
|
Additional options that are not required for publishing a message. |
[options.queue] |
Queue
|
A |
[options.qos] |
Number
|
The MQTT QoS (Quality of Service) setting. Supported Values
Note: this setting is not supported by non mqtt brokers! |
callback |
Function
|
Called once the adapter has successfully finished publishing the message (usually once the buffer is flushed). |
Name | Type | Description |
---|---|---|
err |
Error
|
An error object is included if an error was supplied by the adapter. |
Provide a callback to be called once the Client
is ready to be used.
Name | Type | Description |
---|---|---|
callback |
Function
|
Called once the |
Name | Type | Description |
---|---|---|
err |
Error
|
The connection error (if one occurred). |
Subscribe to the specified topic
or topic pattern.
Topic Patterns
+
wildcard character matches exactly one word.*
wildcard character matches zero or more words./
character.Name | Type | Description |
---|---|---|
topic |
String
|
The topic or topic pattern to subscribe to. |
[options] |
Object
|
Options passed to the adapter. |
callback |
Function
|
Called once the adapter has finished subscribing. |
Name | Type | Description |
---|---|---|
err |
Error
|
An error object is included if an error was supplied by the adapter. |
Unsubscribe from the specified topic
or topic pattern.
Name | Type | Description |
---|---|---|
topic |
String
|
The topic or topic pattern to unsubscribe. |
callback |
Function
|
Called once the adapter has finished unsubscribing. |
Name | Type | Description |
---|---|---|
err |
Error
|
An error object is included if an error was supplied by the adapter. |