-
Notifications
You must be signed in to change notification settings - Fork 2
Service Architecture
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:
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');
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
};
These are components that are built into the parent class, are automatically executed when needed, and I highly recommend you don't override them.
This method is called during Boot to inject the service into the hummingbird app. It does the following:
- Connects to Mediator and loads the service's API credentials, keywords, and accounts.
- Instantiates the Service Stream with the info from Mediator
- Notifies the Callback Responder to allow HTTP requests to that service
- Starts the Service Stream
Service.prototype.boot = function() {
...
}
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);
}
This method connects to relevant EventEmitters from around the app, responding to messages accordingly. Currently, this is:
- Incoming Posts and Deletes from the Service Stream hose.
- A Service Post object is created, and forwarded to the Queue
- A Delete object is restructured into the Deletes format and sent to the Queue
- Incoming commands from Switchboard are forwarded to the command method.
Service.prototype.delegate = function() {
...
}