Class: Bridge()

Forward the events of a connection using the provided client. Also forward the events of the provided client to the connection.

Example

var net = require('net');
var server = net.createServer();

var Adapter = require('strong-pubsub-mqtt');
var client = new Client('mqtt://my.mosquitto.org', Adapter);
var Connection = require('strong-pubsub-connection-mqtt');

server.on('connection', function(connection) {
  var mqttConnection = new Connection(connection);
  var bridge = new Bridge(mqttConnection, client);
});
Class Properties
Name Type Description
connection Connection

The Connection instance provided to the Bridge constructor.

client Client

The Client instance provided to the Bridge constructor.

hooks Object[]

An array hook objects.

Class: Bridge Instance Methods

bridge.before(action, hook)

Add a hook function before the given action is executed.

Arguments
Name Type Description
action String

Must be one of the following:

  • connect
  • publish
  • subscribe
  • unsubscribe
hook Function

The function to be called before the action. Example:

bridge.before('connect', function(ctx, next) {
  if(ctx.auth.password !== '1234') {
    ctx.badCredentials = true;
  }
  next();
});

Action Context

The ctx object has the following properties for the specified actions.

Action: connect

  • ctx.auth - Object containing auth information
  • ctx.auth.username - String containing client username
  • ctx.auth.password - String containing client password
  • ctx.authorized - Boolean Defaults to true. Set to false in a hook to send back an unathorized response.
  • ctx.reject - Boolean Defaults to false. Set to true to reject the action.
  • ctx.clientId - String containing the id of the client.
  • ctx.badCredentials - Boolean Defaults to false. Set to true if the provided credentials are invalid.

Action: publish

  • ctx.topic - String the topic the client would like to publish the message to
  • ctx.message - String or Buffer the message to publish
  • ctx.options - Object protocol specific options
  • ctx.authorized - Boolean Defaults to true. Set to false in a hook to send back an unathorized response.
  • ctx.reject - Boolean Defaults to false. Set to true to reject the action.
  • ctx.clientId - String containing the id of the client.

Action: subscribe

  • ctx.topic - String the topic the client would like to publish the message to
  • ctx.subscriptions - Object containing a topics as keys and options as values. Only ctx.topic or ctx.subscriptions will be set.
  • ctx.options - Object protocol specific options
  • ctx.authorized - Boolean Defaults to true. Set to false in a hook to send back an unathorized response.
  • ctx.reject - Boolean Defaults to false. Set to true to reject the action.
  • ctx.clientId - String containing the id of the client.

Event: unsubscribe

Emitted with a ctx object containing the following.

  • ctx.topic - String the topic the client would like to unsubscribe from.

bridge.connect()

Connect the bridge to the broker using the provided client and connection.