Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created June 16, 2012 20:06
Show Gist options
  • Save 3rd-Eden/2942398 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/2942398 to your computer and use it in GitHub Desktop.
RED API
'use strict';
/**
* Require the RED module.
*/
var RED = require('RED');
/**
* Create a new RED server.
*/
var red = RED.createServer();
/**
* The whole server has been set up, so we can start listing for event
* streams and process them.
*/
red.stream('event name')
// The stream() function will only return the method `validate` and there
// for making it an required flow of your structure, handling invalidated
// data from a client is just.
.validate(function validate(data, callback) {
// Async validation by default of the data before any processing occurs.
// the data is an object with the representation of a client with
// - ip
// - headers
// - etc.
callback(new Error('Un authorized access'), true/false);
})
.async
.map(function map(data, callback){
// async map/reduce appli
})
.sync
.reduce(function reduce(data, memorized) {
// the .sync tells RED that it should not be using a callback based design
})
.worker
.map(function map(data) {
// the .worker prefix allows to move this calculation to a different thread
// than the main node.js thread. If you supply it with a function will use
// that function for the computation if a string is supplied it will assume
// that it's a path to a file that follows the WebWorker API, and it will
// use the on message to return data. The last argument indicates the amount
// of threads we should run for this computation
var hardcore, calculation, on;
return hardcore * calculation - on / data;
}, 10)
.sync
.save('job-name') // store the result or computation in the Engine instance.
.async
.save(function save(data, callback) {
// async name storage is also possible
callback(null, 'async name');
});
/**
* The context method allows you to add methods to the `this` context of your
* red functions. Every item that you store in the `this` value of the function
* will be available in the context of your functions, this allows you to setup
* up database connections and other types of in memory data for RED as you
* won't know in which state your process will run. For example if a instance is
* really busy it might offload the computing power to a new child process.
*
* @TODO figure out a way to detect the busyness of a event loop, for example
* counting the amount of ticks it takes for a setTimeout to fire..
*/
red.context('worker', function worker(done) {
var mongo = require('mongodb');
this.db = mongo.connect();
});
red.context('async', function async(done) {});
red.context('sync', function sync() {
// if no callback is specified we will just wait untill the function is
// finished with executing.. as we can check the functions.length to figure
// out the amount of arguments the function specified
this.hostname = require('os').hostname;
});
/**
* Brain farts:
*
* These are random idea's of possible API's that might be useful to implement
* in the near future.
*/
red.stream('another event')
.validate(function validate(data, callback) {})
.pipe(function pipe(stream) {
// create a pipe method so we can actually pipe the data to a new stream
// insteance, making it easy to write stuff to disk or anything else like
// stdout
stream.pipe(process.stdout);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment