National Park of Minnesota

    sails-hook-redispubsub

    1.1.1 • Public • Published

    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

    Install

    npm i sails-hook-redispubsub

    DownloadsWeekly Downloads

    12

    Version

    1.1.1

    License

    MIT

    Last publish

    Collaborators

    • kos