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 🙏

© 2024 – Pkg Stats / Ryan Hefner

sails-hook-redispubsub

v1.1.1

Published

Sails.js hook implementing redis pub-sub

Downloads

6

Readme

Sails.js Redis PubSub hook

npm version Build Status

This hook is in development and has very limited functionnality please feel free to create issues and new features requests

Main idea of the hook is to give application written with Sails.js ability to communicate with each other using redis Pub/Sub.

Send events

Send global events

Global event will receive all (your) servers that subscribed to given channel.

To send message you could use this method:

// Send message to all sails instances/servers/applications
sails.redispubsub.publish({
  event: 'clearCache',
  items: 'test'
});

or

// Send message to all sails instances/servers/applications
sails.redispubsub.publish('clearCache', {
  items: 'test'
});

Send local events

Local event will be received only by instance that sent event.

Sending local event:

sails.redispubsub.emit('event', { /*... something ... */ });

Note that message that you will send will be converted to string using JSON.stringify please do not send streams, functions in it !

Event handling

Handling global/local events using subscribers

Subscribe on event into your code:

// Note that sails is a global variable
sails.redispubsub.on('event', function(data) {
  // Event handled
  sails.log.debug('event handled', data);
});

And send event from another server (all servers including current will receive event):

sails.redispubsub.publish('event', {
  something: 'anything'
});

Or you could send message only to current instance using emit method: Only current server will receive this message !

sails.redispubsub.emit('event', {
  something: 'anything'
});

Hook is based on default EventEmitter class. So you could use any method from it. List of methods

Note: emit() method will send event only to current instance ! To send event to all instances use publish() method.

Handling all global messages (for all global events)

You could handle all messages using config/redispubsub.js configuration file and onMessage(channel, message) method in it.

Example config/redispubsub.js:


module.exports.redispubsub = {

  onMessage: function(channel, message) {
    console.log(message);
  }
};

module.exports.redispubsub = {

  onMessage: require('../api/services/RedisPubSubService').onMessage //Bind to service
};

Configuration

This hook support redis connection options (full list of options you could find here):


module.exports.redispubsub = {
  /**
   * Redis connection settings
   */
  connection: {

    options: {
      host: 'localhost',
      port: 6370
    }
  },

  /**
   * Channel name of redis pub/sub
   * By default hook use channel name: 'sails'
   *
   * @type {String}
   */
  channel: 'sails-new'

  /**
   * On subscribe handler
   *
   * @type {function}
   */
  onSubscribe: function(channel, count) {
    console.log(channel, count);
  },

  /**
   * Error handler
   *
   * @type {function}
   */
  onError: function(err) {
    console.log(err);
  },

  /**
   * Global message handler
   *
   * @type {function}
   */
  onMessage: function(channel, message) {
    console.log(message);
  }
};

Passing existing pubClient and subClient

You are able to pass existing pubClient and subClient to this hook. So hook wouldn't create a new connections.

in config/redispubsub.js file: This hook support redis connection options (full list of options you could find here):


module.exports.redispubsub = {
  /**
   * Redis connection settings
   */
  connection: {

    pubClient: pubClient,

    subClient: subClient
  }
  // ...
}

In this case you don't need to pass connection.options

License

MIT