Skip to content

Service Architecture

mileszim edited this page Sep 10, 2014 · 1 revision

Every service must be organized into its own folder under /lib/services, with the folder name the same as the service identifier.

Services have the following modules:

Instantiation

A service must require its local Post & Stream modules, and pass them into super during construction. Here's the TwitterService intantiation, for example:

// Modules
var util = require('util');

// Parents
var Service = require('../service.js');

// Local
var Stream   = require('./twitter_stream.js');
var Post     = require('./twitter_post.js');

/**
 * Provides interaction with the Twitter API
 * @constructor
 * @extends Service
 */
var TwitterService = function() {
  Service.call(this, Stream, Post);
}

// Extend Service class
util.inherits(TwitterService, Service);

Afterwards, be sure to include a file loads.js with the following: module.exports = require('./twitter_service.js');

Configuration

Every service must be configured with a SERVICE_NAME identifier, and any other options needed by the API.

TwitterService.prototype.configuration = {
  SERVICE_NAME: 'twitter',
  MAX_KEYWORDS: 400,
  MAX_ACCOUNTS: 5000
};

Components

These are components that are built into the parent class, are automatically executed when needed, and I highly recommend you don't override them.

Boot

This method is called during Boot to inject the service into the hummingbird app. It does the following:

Service.prototype.boot = function() {
  ...
}

Command

This method listens for incoming HTTP command requests from inside (add account, delete keyword, etc) and delegates them to the stream.

Service.prototype.command = function(model, action, param) {
  this.stream.command(model, action, param);
}

Delegate

This method connects to relevant EventEmitters from around the app, responding to messages accordingly. Currently, this is:

Service.prototype.delegate = function() {
  ...  
}