Monitor a connection and automatically reconnect on failure.
Watches your node.js connections and automatically reconnects them when things fail.
Connections in node.js are flaky by nature - a disconnection will throw a fatal error (unless your code is watching for them), potentially bringing down your entire application. Babysitter transparently catches these failures and automatically attempts to reconnect the problem connection, resulting in a more reliable application.
Babysitter can watch any
EventEmitter that emits
var Babysitter = require'babysitter';// Let's give the babysitter something to do besides watch tvvar sitter = Babysitterwatch// This function is responsible for setting up the connection, both// on the initial attempt and any reconnect attempts.var connection = netcreateConnection optionsport optionshost ;// Give the connection back to the babysitter. Note that it's unnecessary to// wait until the connect event for the connection - babysitter handles this transparentlydone null connection ;// Options to be passed to the connect functionhost: 'localhost'port: '8000';// Babysitter emits connect, close, end, and backoffsitteron 'connect'console.log'connect!';;sitteron 'close'console.log'close!';;
All code licensed under the MIT license.