truflux-msg-multiplexer

1.0.0 • Public • Published

Truflux Message Multiplexing

To install

npm install --save truflux-msg-multiplexer

This module attempts to allow multiplexing of truflux message protocols on the same truflux connection.NOTE This module will not work if you are using dynamic protocol ie continuing to add to a protocol after it has already been multiplexed.

Lets try an example. Suppose I have a single socket connection with truflux, but I want multiple protocols to use the socket harmoniously. If they are fixed protocols and they support the required structural interface of a truflux message protocol, then we can multiplex them whilst still using them as we would normally

    
var TrufluxMultiplexMessage = require('truflux-msg-multiplexer'),
    protocol1               = SomeTrufluxMessageInstance,
    protocol2               = SomeOtherTrufluxMessageInstance,
    protocol3               = yetAnotherTrufluxMessageInstance,
    multiplexer             = new TrufluxMultiplexMessage();
 
    multiplexer.setInitial(protocol1)
               .multiplex (protocol2)
               .multiplex (protocol3);
 
    /*
        Now lets assume we've create a truflux socket that uses the multiplexer as its msg parameter
        Since the multiplexer has shuffled the ID values , we can do the following as we would normally
     */
    client.send(protocol1.someMessageID)
          .send(protocol2.someOtherMessageID);
 
    /*
        However we cannot directly index from the id of the multiplexer, because of potential protocol name collisions .Suffice to say because of this you cannot multiplex another multiplexer
    */
 
    //multiplexer.id.someMessageID
 
    /*
        Additionally because of this we cannot do the following because there is no id lookup, 
        so you must provide the number value from the individualized protocols;
    */ 
    client.send('someMessageID');
 
    //DO NOT continue to add more to the truflux message protocols after this 
 

API

We implement the minimum needed functions for the structural interface required of a truflux message

setInitial (otherTrufluxMessage)

/**
 * Sets the inital truflux message object from which the multiplexing is based
 * @param  {TrufluxMessage} otherTrufluxMessage 
 * @return {Multiplexer}     
 */

multiplex (otherTrufluxMessage)

/**
     * Attempts to multiplex the other truflux messages protocol if it has:
     * 
     *      changeMessageID function
     *      id              Object containing a lookup to the ids  
     *
     * So that a single truflux message can encompass all of it but still work individually within their old contexts 
     *
     * @param  {TrufluxMessage} otherTrufluxMessage 
     * @throws {Error}          If otherTrufluxMessage does not support the required functions
     * @return {Multiplexer}                      
     */   

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}       
 */

Package Sidebar

Install

npm i truflux-msg-multiplexer

Weekly Downloads

0

Version

1.0.0

License

none

Last publish

Collaborators

  • titon