Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including mqttr with all npm packages installed. Try it out:

var mqttr = require("mqttr")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

mqttr v2.0.7

A routable mqtt library based on mqtt.js

mqttr NPM version Build Status Dependency Status Coverage percentage

A routable mqtt library based on mqtt.js

Installation

$ npm install --save mqttr

Usage

var mqttr = require('mqttr');

// You should start a mqtt server at 1883 before or after run this script
var client = mqttr.connect('mqtt://localhost');

client.on('connect', function () {
  console.log('connect');
});

client.on('reconnect', function () {
  console.log('reconnect');
});

client.on('close', function () {
  console.log('close');
});

client.on('offline', function () {
  console.log('offline');
});

client.on('error', function (err) {
  throw err;
});

// full params handler
client.subscribe('/users/:userid/message/:messageid/*', function (topic, payload, message) {
  console.log('-------------------------------------------------');
  console.log('topic  :', topic);             // /users/taoyuan/message/4321/ping
  console.log('message:', payload);           // { hello: 'world' }
  console.log('params :', message.params);    // { userid: 'taoyuan', messageid: 4321 }
  console.log('slats  :', message.splats);    // [ 'ping' ]
  console.log('path   :', message.path);      // '/users/:userid/message/:messageid/:method'
  console.log('packet :', message.packet);    // {...} packet received packet, as defined in mqtt-packet
  console.log();
});

// one context param handler
client.subscribe('/users/:userid/message/:messageid/*', function (message) {
  console.log('-------------------------------------------------');
  console.log(message);
  console.log();
});

client.ready(function () {
  client.publish('/users/taoyuan/message/4321/ping', {hello: 'world'});

  setTimeout(function () {
    client.end();
  }, 10);
});

Command Line Tools

mqttr bundles a command to interact with a broker. In order to have it available on your path, you should install mqttr globally:

npm install mqttr -g

Then, on one terminal

mqttr sub -t 'hello' -h 'test.mosquitto.org' -v

On another

mqttr pub -t 'hello' -h 'test.mosquitto.org' -m 'from mqttr'

See mqttr help <command> for the command help.

License

MIT © taoyuan

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free