The app.js Hooks

The hooks are methods exported by the app.js file (your code’s entry point) and are called by the Eko environment while your project is starting up. This is where you’ll add your external dependencies, your logic, your listeners, and anything else you might need to make your project do fun JavaScripty things.

NOTE: When you add code in Eko Studio’s Code Panel, it returns a single function by default; we interpret this as an onInit hook. This was done to make the Code Panel as clear as possible for non-developers.

'use strict';

export default function onInit(player, context) {
    // Visit https://developer.eko.com for detailed API documentation
    player.on('nodestart', function(node) {
        console.log('Node has started: ' + node.id);
    });
};

If you want to export both an onInit and onLoad hook, export an object with the two functions, as an es6 module like so:


export default {
    onInit: function(player, ctx) {
        player.on('nodestart', function(node) {
            console.log('Node has started: ' + node.id);
        });
    },
    onLoad: function(ctx) {
        return new Promise(function(resolve, reject) {
            resolve();
        });
    }
};

Let’s review what the two hooks do:

Note that these hooks receive a context object which holds some useful references like InterludePlayer, loadJS, playerOptions, when, and a few more.

onLoad(ctx)

This method is invoked before the player has been initialized.

You might want to use it to load external libraries that you’ need in your project (you can use loadJS library provided in the ctx object for that).

Note: If your hook is asynchronous and you want it to wait before code execution is continued, your method must return a promise (you can use the when library provided in the ctx object).

onInit(player, ctx)

This method is invoked after the player has been initialized.

Here you can load all the resources needed for playing the project, and start adding your project’s logic.

Note: If your hook is asynchronous and you want the code to wait before continuing, your method should return a promise (you can use the ‘when’ library provided in the ctx object).

Rate this page: X
Tell us more!