cocker

Cocker, a socket module with reconnection logic.

npm install cocker
54 downloads in the last week
135 downloads in the last month

Cocker

build status

A socket module with re-connection logic.

For nodeJS versions < v0.10.x, check v0.8.x branch.

Install

$ npm install cocker [-g]

require:

var Cocker  = require( 'cocker' );

Run Tests

$ cd cocker/
$ npm test

Constructor

Create an instance. Arguments within [ ] are optional.

Cocker( [ Object opt ] ) : Cocker
// or
new Cocker( [ Object opt ] ) : Cocker

Options

Cocker supports all net.Socket options in a unique configuration object:

    // default options are listed
    var options = {
        port : 6379,
        host : 'localhost',
        // 'utf8', 'utf16le' ('ucs2'), 'ascii', or 'hex'.
        encoding : null,
        // false, or initialDelay in ms
        keepAlive : false,
        // millis to emit timeout event
        timeout : 2000,
        /*
         * noDelay, it defaults to false.
         * true for disabling the Nagle algorithm 
         * ( no TCP data buffering for socket.write )
         */
        noDelay : false,
        // unix socket domain file descriptor - path
        fd : undefined,
        // 'tcp4', 'tcp6', or 'unix'
        type : null,
        /*
         * By setting allowHalfOpen = true, the socket will not
         * automatically end()s its side, allowing the user to write
         * arbitrary amounts of data, with the caveat that the user is
         * required to end() his side now.
         */
        allowHalfOpen : false,

        // Cocker reconnection options
        // logging to console
        debug : false
        // try 3 times before quitting
        attempts : 3,
        // millis, default to 1 sec
        retryInterval : 1000,
        // interval between attempts = retryInterval * Math.pow( attempt, retryFactor )
        retryFactor : ( Math.sqrt( 5 ) + 1 ) / 2
    };

Properties

All the properties from net.Socket module are inherited.

also :

// a property that holds the initial config object:
Cocker.options : Object

// current number of connection attempts
Cocker.attempts : Number

// a flag, also useful to manually disable/re-enable/check reconnection-loop
Cocker.lost : Boolean

Methods

All the methods from net.Socket module are inherited. Arguments within [ ] are optional.

Cocker methods:

// connect optionally with a config object, like for net.Socket constructor.
Cocker#run( [ Object opt ] ) : undefined

// write to socket, encoding defaults to 'utf8'
Cocker#send( Buffer data || String msg [, String enc ] ) : Boolean

// end the connection
Cocker#bye( Buffer data || String msg [, String enc ] ) : undefined

// emit an event, if debug was on, it will log event to console
Cocker#bark( String evt [, arg1 [, arg2 [, .. [, argN ] ] ] ] ) : undefined

Events

All the events from net.Socket module are inherited.

Cocker events :


// connection is established ( on 'connect' event )
'online' : function ( Object address, Number timestamp ) : undefined

/*
 * connection is down ( on 'close' event )
 * now it will try to reconnect opt.attempts times.
 */
'offline' : function ( Number timestamp ) {}

// k is the number of current connection attempt
'attempt' : function ( Number k, Number timestamp, Number millis ) : undefined

// connection is definitively lost ( after opt.attempts times )
'lost' : function ( Number timestamp ) {}

/*
 * commands are not written to socket, but buffered in memory
 * ( the socket connection is slow or not fully established ).
 * 'drain' will be emitted when the buffer is again free.
 */
'slowdown' : function ( String readyState, Number bufferSize ) {}

// informational event for logging
'info' : function ( String msg ) {}

// warning event for logging
'warning' : function ( String  msg ) {}

// signal socket timeout
'timeout' : function ( Number timestamp ) {}

other events from net.Socket:



'connect' : function () {}

'close' : function ( Boolean hadError ) {}

// old API for streams ( nodeJS < v0.10.x )
'data' : function ( Buffer data ) {}

// new stream2 API ( nodeJS >= v0.10.x )
'readable' : function () {}

'end' : function () {}

'drain' : function () {}

'error' : function ( Error err ) {}

'close' : function ( Boolean had_error ) {}

MIT License

Copyright (c) 2012 < Guglielmo Ferri : 44gatti@gmail.com >

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you