reconnect robustness




uber simple reconnect logic for tls or tcp.


The process of reconnecting a client should be smarter than just re-trying the same server repeatedly. A client should attempt to round-robin connection attempts to a series of servers running on different subnets (or at least in different data centers) and exponentially back off from connections that fail.


var restream = require('restream');
var options = {
  reconnectTime: 128, // time before attempting to reconnect 
  connectTime: 1024,  // max time spent attempting to connect 
  servers: [
    { port: 9960, host: '', },
    { port: 9968, host: '', },
    { port: 9020, host: '', },
    { port: 9967, host: '' }
var client = restream.connect(options)
client.on('connect', function(connection) {
  // ...connected, event may occur many times. 
client.on('error', function(errporthost) {
  // ...could not connect to a specific server. 
client.on('fail', function() {
  // ...could not connect to any servers. 


The options object also accepts the regular options that are accepted by the tls.connect andtcp.connect` methods. Method returns an event emitter.

Specify if something other than tcp should be used.

An optional number to determine how long before attempting the next reconnection. Defaults to 3e4. Note that this number is multiplied by the number of failures to connect.

An optional number to determine how long before a simple connection attempt should be timed out.

An array that contains a list of servers. Each object in the array should contain a port and host address. When a connection can't be made to a server, the next server in the array is selected for the reconnect attempt.

How many times to cycle though the start list before calling the onFail method.