node-connection-manager
Store and retreive active connections from multiple locations in your code-base, keep connection alive as long as there are references to it. Able to reconnect and manage listeners.
var cm = require('connection-manager')('MY_APPS_CONNECTIONS', {
id: 'unique_to_this_object_instance',
foo: 'bar',
hello: ['country', 'world', 'universe']
});
cm.create({
id: 'irc://user@irc.freenode.net',
timeout: 10000,
credentials: {
nick: 'user',
host: 'irc.freenode.net'
},
connect: function (cb) {
conn.on('connect', function (connection) {
cb(null, connection);
});
conn.on('error', function (err) {
cb(err);
});
conn.open(this.credentials);
},
listeners: {
join: function (object) {
console.log(this.scope.hello);
},
leave: function (object) {
console.log(this.scope.foo);
}
},
addListener: function (name, func) {
this.connection.on(name, func);
},
removeListener: function (name, func) {
this.connection.off(name, func);
},
disconnect: function (cb) {
this.connection.close();
cb();
}
},
function (err, client) {
if (err) {
}
console.log('id: ' + client.id);
client.connection.send('this is a pretend message for a pretend connection');
console.log('references: ' + client.references);
});
Elsewhere in the code:
var connectionManager = require('connection-manager');
var cm = connectionManager('MY_APPS_CONNECTIONS', {
id: 'this_is_unique_to_this_object_instance',
foo: { bar: 'baz' },
hello: 'goodbye'
});;
var credentials = {
nick: 'user',
host: 'irc.freenode.net'
};
var client = cm.get('irc://user@irc.freenode.net', credentials);
console.log('references: ' + client.references);
client.connection.send('this is another pretend message');