Class: Client(options, Adapter, [transport])

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 bridges 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);

Events

Event: connect

Emitted on successful connection (or reconnection).

Event: error

Emitted when a connection could not be established.

Arguments
Name Type Description
options Object

See client.options below.

Adapter Function

The adapter constructor.

[transport] Object

The optional transport module implementing require('net').createConnection().

Class Properties
Name Type Description
options Object

The options passed as the first argument to the Client constructor.

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. require('net')) if passed to the Client constructor.

Class: Client Instance Methods

client.connect(callback)

Connect to the broker or bridge.

Arguments
Name Type Description
callback Function

Added as a once listener to the connect event.

Callback
Name Type Description
err Error

A connection error (if one occured).

client.end(callback)

Disconnect from the broker or bridge.

Arguments
Name Type Description
callback Function

Added as a once listener to the connect event.

Callback
Name Type Description
err Error

A connection error (if one occured).

client.publish(topic, message, [options], callback)

Publish a message to the specified topic.

Arguments
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 Queue object to bind the topic to. Only supported using the STOMP adapter.

[options.qos] Number

The MQTT QoS (Quality of Service) setting. Supported Values

  • 0 - Just as reliable as TCP. Client will not get any missed messages (while it was disconnected).
  • 1 - Client receives missed messages at least once and sometimes more than once.
  • 2 - Client receives missed messages only once.

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).

Callback
Name Type Description
err Error

An error object is included if an error was supplied by the adapter.

client.ready(callback)

Provide a callback to be called once the Client is ready to be used.

Arguments
Name Type Description
callback Function

Called once the Client is in a ready state or an error has occurred.

Callback
Name Type Description
err Error

The connection error (if one occurred).

client.subscribe(topic, [options], callback)

Subscribe to the specified topic or topic pattern.

Topic Patterns

  • The + wildcard character matches exactly one word.
  • The * wildcard character matches zero or more words.
  • Wildcards can be used in combination.
  • Words are separated by the / character.
Arguments
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.

Callback
Name Type Description
err Error

An error object is included if an error was supplied by the adapter.

client.unsubscribe(topic, callback)

Unsubscribe from the specified topic or topic pattern.

Arguments
Name Type Description
topic String

The topic or topic pattern to unsubscribe.

callback Function

Called once the adapter has finished unsubscribing.

Callback
Name Type Description
err Error

An error object is included if an error was supplied by the adapter.