Core
Write Plugins v1.x
1

You are currently looking at the documentation of a previous version of Kuzzle. We strongly recommend that you use the latest version. You can also use the version selector in the top menu.

Hooks #

Hooks are asynchronous listeners, plugged to events, and receiving information regarding that event.

Hooks can only listen: the received information cannot be changed. And Kuzzle doesn't wait for their execution either, so hooks cannot change the outcome of whatever triggered the listened event.


Usage #

Plugins can register hooks by exposing a hooks object: keys are listened events, and values are either a function to execute whenever that event is triggered, or an array of functions.

Copied to clipboard!
this.hooks = {
  '<kuzzle event to listen>': <function to call>,
  '<another event>': [list, of, functions, to call]
};

Example #

Copied to clipboard!
module.exports = class HookPlugin {
  constructor() {}

  /*
   Required plugin initialization function
   (see the "Plugin prerequisites" section)
   */
  init(customConfig, context) {
    /*
      Calls the plugin functions when the Kuzzle events
      are
     */
    this.hooks = {
      'core:kuzzleStart': this.myFunctionOnStart.bind(this),
      'document:afterCreate': this.myFunctionOnCreate.bind(this)
    };
  }

  /*
  Called whenever the "document:afterCreate" event
  is triggered
  */
  myFunctionOnCreate(request, event) {
    console.log(`Event ${event} triggered`);
    console.log(`Document created: ${request.result._id}`);
  }
};