truflux-msg

2.1.0 • Public • Published

Truflux Message

To install

npm install --save truflux-msg

This is the definition module for truflux https://www.npmjs.com/package/truflux

The truflux message definition is designed to be seperate from the actual messaging system to allow you to seperate protocol from implementation

New in v2.0.0

To provide better decoupling, truflux-msg now no longer supports the enforce Schema methods any more, which have been moved to a separate module, truflux-msg-schema. The prebuilt browser versions have removed to allow one to include their own specialized build if need be

Example

//In your protocol definition file lets call it protocol.js
var Message  = require('truflux-msg'),
    protocol = new Message({noThrow:true});
 
protocol.add('error')
        .add('echo' );
 
module.exports = protocol;
//Now for your implementation
var protocol = require('./protocol'),
    truflux  = require('truflux');
 
protocol.specify("echo",function(data,socket)
{
    console.log(data);
});
 
 
var Client= new truflux.Client({host:"localhost",msg:protocol}));
 
Client.connect()
      .on('connected',function(sock)
      {
            sock.send('echo','Atreyuuuu!');
            //Alternatively, you can reference the message ID directly which is preferable as you do not need a string lookup
            sock.send(protocol.id.echo,'Atreyuuu');
      });
 ...
 

API

In case it wasn't apparent from above , the preferred method of accessing the integer value of a message is to use msgObjectInstance.id.identifierName , this can be used to obtain values for has and call

Constructor(options)

/**
 * Creates a new TrufluxMessage
 * @param {Object}  [options] 
 * @param {Boolean} [options.noThrow] If true, we will not throw an error for unimplemented functions for "add" 
 */

add(identifier,[func])

/**
 * Adds a function handler for the provided string
 * @param  {String} identifier 
 * @param  {Function} [func]  function(data,socket)   
 * @return {TrufluxMessage}    
 */

specify(identifier,func)

    /**
     * Specifies the function to be used for the provided identifier. Used to override add
     * @param  {String|Number} identifier 
     * @param  {Function} func function(data,socket)       
     * @return {TrufluxMessage}            
     */

call(id,payload,socket)

/**
 * Calls the handler associated with the message ID. NOTE This will throw an error if there is no 
 * handler associated. It is assumed you have called "has" before hand
 * @param  {Number} id      
 * @param  {Any} payload 
 * @param  {TrufluxSocket} socket 
 * @return {TrufluxMessage}   
 */

has(messageID)

/**
 * Checks to see if it has the specified message handler
 * @param  {Number}  messageID 
 * @return {Boolean}       
 */

changeMessageID(oldId,newID)

/**
 * Changes the ID of a message handler. Ideal if you need to make sure that the ID has a certain value 
 * @param  {Number|String} oldId The old identifer of the handler
 * @param  {Number} newID  The new ID value 
 * @throws {Error } If the newID is already taken
 * @return {TrufluxMessage}       
 */

Package Sidebar

Install

npm i truflux-msg

Weekly Downloads

9

Version

2.1.0

License

none

Last publish

Collaborators

  • titon