node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org ยป


ConvoJS: making chatty protocols easier

ConvoJS is made to make chatty protocols over streams easier, now you are able to create a protocol with an object.

How it works

Step 1: Install ConvoJS

you can include the convo.js file or install it via NPM

npm install convojs

Step 2: Code

now you need to load ConvoJS first in an var,

var convojs = require('convojs');

ConvoJS only returns a function this is the constructor so creating a new ConvoJS object is done like this

var convo = new convojs();

now this will obviously throw errors because there are no arguments, ConvoJS only needs one arguments and that is the options object which usually looks like this

var convo = new convojs({
    // the stream we need to listen AND write to
    stream  : serialPort,
    // the commands for this convo
    commands: [
            // the solver of this command, can be a string,regex,function or array
            // with those things
            // response can be everything, if its not a function it will be directly
            // be written to the stream when this command is called
            response:"Hey gtg kthxbye",
            // the commands that can be executed after this command
                    // solver as regex
                    // if the solver is regex and the repsonse a string, the string
                    // will be formatted with the matches
                    // for example if you send 'hello' this command will respond with
                    // You told me 'hello'
                    response:"You told me '{0}'",
                            solver:'cant get in',
                            // if commands is false we will not scope deeper and
                            // stay in the current command collection
                            // solver also can be a function
                            // there will be passed 1 argument and thats the data
                            // solver must return something truety or falsy
                                return !!data;
                            // repsonse also can be a function
                            // there will be passed 3 arguments
                            // - writer: the stream to write to
                            // - data: the data received from the stream
                            // - solver: the solver

                            // commands can be a negative number,
                            // when commands is a negative number it will go up
                            // that amount of times so in this example the
                            // command collection will be set to the collection
                            // it started with
    // [optional] works just like the command collection but will always be fired
    // BEFORE the standard command collection, if a command is matched
    // it will immeditially stop looking for others
    // [optional] where ConvoJS has to write the reponses to
    //  writer has an higher priority then stream
    // [optional] where ConvoJS has to read from
    //  reader has an higher priority then stream

ConvoJS will automagicly hook to the data event so you only need to build the object and (if needed) open the stream

Object Reference


this will unbind the ConvoJS from the stream and release all events hooked to it


the given options, merged with the default options object


the stream where ConvoJS writes to


the stream where ConvoJS reads from


all the global commands in this ConvoJS


the given commands object


the at the moment selected command