Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
joshjung edited this page Nov 11, 2014 · 5 revisions

Starting in Pomelo 0.6, anyone can build custom plugins for Pomelo.

Structure

Pomelo plugins are divided into two sections: components and events. Components are necessary while events are optional:

plugin

For more information on component architecture, see 'What is a component' here. In Pomelo, a component is a reusable factory where each instance provides a service. A plugin can have several components, and developers can implement different life cycle callbacks (e.g. start, afterStart, stop, etc.).

Developers can deal with events in pomelo using events in plugin. The events include add_servers, remove_servers, replace_servers, bind_session, close_session.

  • add_servers:add server event,parameter is server information, parameter type is array.
  • remove_servers: remove server event,parameter is server information,parameter type is array.
  • replace_servers: servers disconnect from intranet(not master) and reconnect event, parameter is servers information which do not disconnect from intranet, parameter type is array.
  • bind_session: bind session event, parameter is session object.
  • close_session: close session event(including connection close and error), parameter is session object.

How to use

###API

####app.use(plugin, opts) use plugin in pomelo ####Arguments

  • plugin - plugin that uses in pomelo
  • opts - attach parameters

In pomelo only need to configure in app.js, the example code is as follow:

javascript var statusPlugin = require('pomelo-status-plugin');

app.use(statusPlugin, { status:{ host: '127.0.0.1', port: 6379 } });


##How to build

First, you need to create a plugin project, the directories is as follow diagram:

![plugin-dir](http://pomelo.netease.com/resource/documentImage/plugin-dir.png)

Second, you need to configure the components and events in index.js, and the example code is as follow:

```javascript```
module.exports = {
 components: __dirname + '/lib/components/',
 events: __dirname + '/lib/events/'
};

At last, you can add your components and events in plugin. For components, you need to export its constructor, the example code is as follow:

javascript module.exports = function(app, opts) { return new Component(app, opts); };

var Component = function(app, opts) { //do construction };

Component.prototype.start = function(cb) { // do something application start };

Component.prototype.afterStart = function(cb) { // do something after application started };

Component.prototype.afterStart = function(cb) { // do something on application stop };


For events, it is the same as components, the example code is as follow:

```javascript```
module.exports = function(app) {
 return new Event(app, opts);
};

var Event = function(app) {
 //do construction
};

Event.prototype.add_servers = function(servers) {
 //do something when application add servers
};

Event.prototype.remove_servers = function(ids) {
 //do something when application remove servers
};

Event.prototype.replace_servers = function(servers) {
 //do something when server reconnected
};

Event.prototype.bind_session = function(session) {
 //do something when session binded
};

Event.prototype.close_session = function(session) {
 //do something when session closed
};
Clone this wiki locally