Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mattijs committed Apr 3, 2012
0 parents commit 77aa8bb
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Redubber

Redubber can be used to control multiple Redis PubSub servers through
[redub](https://github.com/two-screen/redub) and
[redis-pubsub](https://github.com/two-screen/redis-pubsub).

## Usage

Redubber can be used from the terminal. It requires a configuration file to tell it what
servers and channels to listen to. This will file is simple JSON file containing a list of
servers and a list of channels:

```json
{
"channels": [ "message" ],
"servers": [ "localhost:6379" ]
}
```

## Monitor

You can monitor messages received on multiple Redis servers from the CLI

```bash
$ redubber monitor
```

## Send

Messages can be send from the commandline as arguments to the send command:

```bash
$ redubber send "a message"
```

### Options

- `-c|--channel CHANNEL`: The channel to send the message to
- `-j|--json`: Treat input as json and parse it
- `-a`: Read input from STDIN

Messages can also be read from `STDIN` by piping it into `redubber`:

```bash
$ cat message.json | redubber send -j -a
```

# License

(The MIT License)

Copyright (c) 2012, Two Screen

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.
150 changes: 150 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
var _ = require('underscore'),
pubsub = require('redis-pubsub'),
Redub = require('redub');

// DEFAULTS
var DEFAULT_PORT = 6379;
var DEFAULT_HOST = 'localhost';

/**
* Parse a host:port spec into a separate values. Defaults will always be supplied
* @param {String} address
* @return Object
*/
function parseAddressSpec(address) {
var addressSplit = address.split(':', 2);

return {
port: addressSplit[1] || DEFAULT_PORT,
host: addressSplit[0] || DEFAULT_HOST
};
}

/**
* Spy on one or more redis servers
* @param {Array} servers
*/
function Redubber(servers) {
this.connections = []; // Server connections
this.channels = []; // Channel names across all connections

// Redub for managing multiple servers
this.redub = new Redub();

// Parse servers passed to this constructor
(servers || []).map(Redubber.prototype.addServer, this);
}

Redubber.prototype.listen = function(listener) {
var redub = this.redub;
this.channels.forEach(function(channel) {
redub.on('message', function(message) {
listener(channel, message);
});
});
};

/**
* Add a server to the Redis Spy
* @param {Object|String} config
* @return Redubber
*/
Redubber.prototype.addServer = function(config) {
var server = this.parseServer(config);
if (false === server) {
return this;
}

var channelNames = [], redub = this.redub;
server.channels.forEach(function(config) {
// Create a new PubSub channel
var channel = pubsub.createChannel(server.port, server.host, config.name);
//channel.raw = config.raw;

channel.on('ready', function() {
console.log('Connection to ' + server.host + ':' + server.port + ' is ready');
});

// Add it to Redub
redub.add(channel);

// Register the channel name
channelNames.push(config.name);
});

// Filter channel names
this.channels = _(this.channels).union(channelNames);

return this;
};

/**
* Parse a server configuration into a server object
* @param {Object} config
* @return Object
*/
Redubber.prototype.parseServer = function(config) {
var server = {}, address, channels;

// String type server config
if (typeof(config) === 'string') {
// Split channels from address
var channelSplit = config.split('#', 2);

// Parse the address
address = parseAddressSpec(channelSplit[0]);
server.port = address.port || DEFAULT_PORT;
server.host = address.host || DEFAULT_HOST;

// Split channels
channels = (channelSplit[1] || '').split(',').map(function(channel) { return channel.trim(); });
}
// Server config object
else {
if (config.host) {
server.host = config.host;
server.port = config.port || DEFAULT_PORT;
}
else if (config.address) {
address = parseAddressSpec(config.address);
server.port = address.port || DEFAULT_PORT;
server.host = address.host || DEFAULT_HOST;
}
else {
return false;
}

//address = server.address;
channels = config.channels || [];
}

// Parse all channels
server.channels = [];
channels.forEach(function(channel) {

// Channel as object
if (typeof(channel) === 'object') {
if (!channel.name) return;

// Push the channel on the stack
server.channels.push({ "name": channel.name, "raw": channel.raw || false });
}
else if (typeof(channel) === 'string') {
// Check for raw option
var raw = false;
if ((channel.length - 4) === channel.indexOf(':raw')) {
raw = true;
channel = channel.substring(0, channel.length - 4);
}

// Push the channel on the stack
server.channels.push({ "name": channel, "raw": raw });
}
});

return server;
};


// our awesome export products
exports = module.exports = Redubber;
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "redubber",
"description": "Control multiple Redis PubSub servers through redub and redis-pubsub",
"version": "0.0.1",
"homepage": "https://github.com/two-screen/redubber",
"author": "Mattijs Hoitink <mattijs@monkeyandmachine.com> (http://monkeyandmachine.com)",
"keywords": ["redis", "pubsub", "redub"],
"repository": {
"type": "git",
"url": "git://github.com/two-screen/redubber.git"
},
"license": "MIT",
"dependencies": {
"redis-pubsub": "*",
"redub": "*",
"underscore": ">=1.3"
},
"main": "./index.js",
"bin": {
"redubber": "./bin/redubber"
},
"engines": {
"node": "0.6"
}
}

0 comments on commit 77aa8bb

Please sign in to comment.