npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

sails-hook-mqtt

v1.0.6

Published

mqtt client for sails app.

Readme

sails-hook-mqtt

There are no updates planned for this hook for Sails v1.0 and beyond.

Feel free to continue to use this hook in existing projects, as-is, as long as it's doing the job for you. Just note that it's no longer the approach the Sails core team uses for new apps. Instead, we are now recommending the approach for publish & subscribe mqtt to topics that is bundled as part of sails new in Sails v1.

To try that out, run sails new foo --caviar using Sails >= v1.0 and Node <= v7.9.

If you're unsure or need advice, visit https://sailsjs.com/support.

Dependency Status

Email hook for Sails JS, using MQTT.js

Note: This requires Sails v0.10.6+.

Installation

npm install sails-hook-mqtt After installation it create,

  1. config/mqtt.js, Mqtt Configuration
  2. mqtt/hander.js, Event hander for mqtt client

Usage

sails.hooks.mqtt.publish(topic, message, cb)

Parameter | Type | Details -------------- | ------------------- |:--------------------------------- topic | ((string)) | Topic is the topic to publish to message | ((string)) | Data to use to replace template tokens cb | ((function)) | Callback to be run after publish message (or if an error occurs).

sails.hooks.mqtt.subscribe(topic, cb)

Parameter | Type | Details -------------- | ------------------- |:--------------------------------- topic | ((string)) | Topic is the topic to publish to cb | ((function)) | Callback to be run after subscribe message (or if an error occurs).

Configuration

By default, configuration lives in sails.config.mqtt or config/mqtt.js. The configuration key (mqtt) can be changed by setting sails.config.hooks['sails-hook-mqtt'].configKey.

broker

Broker URL

Parameter | Type | Details -------------- | ------------------- |:--------------------------------- brocker | ((string)) | brocker url, example mqtt://127.0.0.1

connect

connection perameters

Parameter | Type | Details -------------- | ------------------- |:--------------------------------- host | ((string)) | any hostname or ip port | ((string)) | proker port no. clientId | ((string)) | Uniqe ID for mqtt client username | ((string)) | the username required by your broker, if any password | ((string)) | the password required by your broker, if any resubscribe | ((string)) | if connection is broken and reconnects, subscribed topics are automatically subscribed again (default true) reconnectPeriod | ((string)) | 1000 milliseconds, interval between two reconnections connectTimeout | ((string)) | 30 * 1000 milliseconds, time to wait before a CONNACK is received queueQoSZero | ((string)) | if connection is broken, queue outgoing QoS zero messages (default true) clean | ((string)) | true, set to false to receive QoS 1 and 2 messages while offline will | ((string)) | a message that will sent by the broker automatically when the client disconnect badly, format in example. key | ((string)) | absolute path to key cert | ((string)) | absolute path to cert ca | ((string)) | absolute path to ca handler | ((object)) | handler to manage the events.

publishOptions

option on publish message

Parameter | Type | Details -------------- | ------------------- |:--------------------------------- qos | ((string)) | QoS level, Number, default 0 retain | ((string)) | retain flag, Boolean, default false dup | ((string)) | mark as duplicate flag, Boolean, default false

subscribeOptions

option on subscribe topic

Parameter | Type | Details -------------- | ------------------- |:--------------------------------- qos | ((string)) | QoS level, Number, default 0

topics

Array of default topics (string)

Example

// [your-sails-app]/config/mqtt.js

module.exports.mqtt = {
  _hookTimeout: 20000,
  broker : 'mqtt://127.0.0.1',                        // broker url
  connect:{                                           // connection config
    host : '127.0.0.1',                         // host name <optional></optional>
    port : 8883 ,                                   // port no.
    clientId : 'sails_hook_mqtt_client',            // client id
     protocolId: 'MQTT',                            // protocol id (optional)
    username:'test_client',                         // mqtt basic auth (optional)
    password:'public',                              // mqtt basic auth (optional)      
    resubscribe: true,                              // (optional)
    reconnectPeriod:1000,                           // (optional)
    connectTimeout:30*1000,                         // (optional)
    queueQoSZero:true,  //if connection is broken, queue outgoing QoS zero messages (default true)(optional)
    will:{                                          // a message that will sent by the broker automatically when the client disconnect badly (optional)
      topic:"server/disconnect",
      payload:JSON.stringify({msg:'i am off-line'}),
      qos:1,
      clean:false
    },
    key : 'path_to/server.key',                    // Absolute path to key (optional)
    cert :'path_to/server.crt',                   // Absolute path to cert (optional)
    ca :  'path_to/ca.crt',                        // Absolute path to ca (optional)
    clean: false,                                   // (optional)
  },
  publishOption:{                                   // default option to subcribe topics
    qos:1,                                          // QoS level, Number, default 0 (optional)
    retain : true,                                  //retain flag, Boolean, default false (optional)
    dup : false                                     // mark as duplicate flag, Boolean, default false (optional)
  },
    subscribeOption:{                                   // default option to subcribe topics (optional)
    qos:1                                         // QoS level, Number, default 0 (optional)
  },
  topics : [                                          // default topics to subcriblbe
    "topic/#"
  ],
  handler : require('../mqtt/handler.js')             // handler object
}

Example

executing the following command (after configuring for your mqtt service and turning off test mode) :

// PUBLISH MESSAGE TO TOPIC OR TOPICS(`array of topics`)

sails.hooks.mqtt.publish('topic/any',"Hi, I am published message",function(err){
  if(err){
    sails.log.error(new Error(err));
  }
  sails.log.debug('Message published successfully');
});

//SUBSCRIBE TOPIC OR TOPICS(`array of topics`)
sails.hooks.mqtt.subscribe('topic/#',function(err, granted){
  if(err){
    sails.log.error(new Error(err));
  }
  sails.log.debug('Topic subscribe successfully');
});