---
title: index.js methods
description: The methods Volumio calls on a plugin's class in index.js, with their arguments and return values, and the command router calls a plugin makes into Volumio.
---

`index.js` exports one class. Volumio creates an instance with `new` and calls the methods below. This page covers plugin API 1, the API Volumio 4 runs. The Browse, play and search methods of a `music_service` are in [Music service API](/plugins/reference/music-service-api). [How a plugin runs](/plugins/concepts/how-a-plugin-runs) explains the lifecycle.

Every method Volumio calls returns a kew promise (`require('kew')`), except `constructor` and `getConfigurationFiles`. An `async` method or a native `Promise` fails Volumio's check.

## Methods Volumio calls

| Method | Required | Volumio calls it | Returns |
| --- | --- | --- | --- |
| [`constructor(context)`](#constructorcontext) | Yes | When it loads an enabled plugin | The instance |
| [`getConfigurationFiles()`](#getconfigurationfiles) | Yes | Before `onVolumioStart` | An array of file names |
| [`onVolumioStart()`](#onvolumiostart) | Yes | Right after loading | kew promise |
| [`onStart()`](#onstart) | Yes | After `onVolumioStart` | kew promise |
| [`onStop()`](#onstop) | Yes | When the user disables the plugin | kew promise |
| [`getUIConfig()`](#getuiconfig) | With a settings page | When the user opens the settings page | kew promise of the page |
| [The `onSave` method](#the-onsave-method) | No | When the user selects **Save** on the settings page | kew promise |
| [`onVolumioShutdown()`](#onvolumioshutdown-onvolumioreboot) | No | Before the device powers off | kew promise |
| [`onVolumioReboot()`](#onvolumioshutdown-onvolumioreboot) | No | Before the device reboots | kew promise |

### `constructor(context)`

Volumio passes a `context` object. Keep it and do no other work.

| Property | Holds |
| --- | --- |
| `context.coreCommand` | The command router. The template keeps it as `this.commandRouter`. |
| `context.logger` | The Volumio logger. The template keeps it as `this.logger`. |

```js
constructor(context) {
  this.context = context;
  this.commandRouter = context.coreCommand;
  this.logger = context.logger;
}
```

### `getConfigurationFiles()`

Returns the configuration files Volumio copies to `/data/configuration/<type>/<id>/` the first time the plugin runs. Returns a plain array, not a promise.

```js
getConfigurationFiles() {
  return ['config.json'];
}
```

### `onVolumioStart()`

Loads the saved settings with `v-conf`, from Volumio's own copy at `/volumio/node_modules/v-conf`. Resolves quickly: at boot Volumio waits at most 30 seconds for all plugins.

```js
onVolumioStart() {
  this.config = new VConf();
  this.config.loadFile(this.commandRouter.pluginManager.getConfigurationFile(this.context, 'config.json'));
  return libQ.resolve();
}
```

`this.config.get(key)` reads a setting. `this.config.set(key, value)` writes it and saves it to disk.

### `onStart()`

Starts the plugin's work: connections, systemd units, Browse entries. Resolves quickly and does long work afterwards. At boot Volumio waits at most 30 seconds for all plugins' `onStart`, then goes on without the slow ones. A rejected `onStart` leaves the plugin failed.

### `onStop()`

Undoes everything `onStart` did: stops timers, closes sockets, stops child processes and systemd units, removes Browse entries.

### `getUIConfig()`

Resolves the settings page: `UIConfig.json` with its text translated and the saved values filled in. See [Add a settings page](/plugins/how-to/settings-page).

```js
getUIConfig() {
  const lang = this.commandRouter.sharedVars.get('language_code');
  return this.commandRouter
    .i18nJson(this.file(`i18n/strings_${lang}.json`), this.file('i18n/strings_en.json'), this.file('UIConfig.json'))
    .then((uiconf) => {
      uiconf.sections[0].content[0].value = this.config.get('greeting');
      return uiconf;
    });
}
```

### The `onSave` method

The method named in a section's `onSave`, in `UIConfig.json`. It gets one object, `{ <field id>: value }`, with the fields listed in the section's `saveButton.data`.

```js
saveSettings(data) {
  this.config.set('greeting', data.greeting);
  return libQ.resolve();
}
```

A button with `onClick` and a modal button with `emit: 'callMethod'` call a plugin method the same way, with their `data` as the argument.

### `onVolumioShutdown()`, `onVolumioReboot()`

Optional. Volumio calls them before it powers off or reboots the device. Use them to save state that would otherwise be lost.

## Calls into Volumio

A plugin calls Volumio through `this.commandRouter`. These calls make up plugin API 1:

| Call | Does | Returns |
| --- | --- | --- |
| `pushToastMessage(type, title, message)` | Shows a toast. `type` is `success`, `info`, `warning` or `error`. | kew promise |
| `broadcastMessage(event, payload)` | Sends a message to every open Volumio UI, such as `openModal` | kew promise |
| `sharedVars.get('language_code')` | The user's language, such as `en` | string |
| `pluginManager.getConfigurationFile(context, name)` | The path of a configuration file on the device | string |
| `i18nJson(languageFile, englishFile, jsonFile)` | `jsonFile` with its `TRANSLATE.` keys translated, English for missing keys | kew promise |
| `executeOnPlugin(type, id, method, data)` | Calls a method of another plugin | What the method returns, `undefined` when the plugin is not installed |
| `volumioAddToBrowseSources(source)` | Adds a `music_service`'s entry to Browse | |
| `volumioRemoveToBrowseSources(name)` | Removes a Browse entry by name | |
| `volumioUpdateToBrowseSources(name, source)` | Changes a Browse entry in place | |
| `servicePushState(state, id)` | Tells Volumio what a `music_service` that plays by itself is playing | |

Plugins that need them also use these calls:

| Call | Does | See |
| --- | --- | --- |
| `addDSPSignalPathElement(element)` | Reports the plugin as an element of the signal path | [Report DSP elements](/plugins/how-to/dsp-elements) |
| `removeDSPSignalPathElement({ id })` | Removes that element | [Report DSP elements](/plugins/how-to/dsp-elements) |
| `registerThirdPartyUI({ uiPrettyName, uiName, uiPath })` | Adds a user interface users can pick | [Add your own user interface](/plugins/how-to/own-user-interface) |

A plugin never assigns to a command router method and never loads Volumio's own files with `require`.

## The logger

`this.logger` has four methods: `info`, `warn`, `error` and `debug`. Each takes one string. See [Log from a plugin](/plugins/how-to/logging).

## Related

- [Music service API](/plugins/reference/music-service-api)
- [UIConfig.json](/plugins/reference/uiconfig-json)
