How a plugin runs
A Volumio plugin is a JavaScript class in index.js. Volumio creates one instance of it when the plugin is enabled, and keeps it while the device runs. It calls the instance's methods at fixed moments: at boot, when the user opens the settings page, when the user plays a track. Every method Volumio calls returns a kew promise. The plugin talks back to Volumio through one object, the command router.
This page describes plugin API 1, the API Volumio 4 runs. A plugin declares it with "engines": { "api": "1" } in volumio-plugin.json.
Plugin types
Every plugin has one type. The type decides where the plugin lives on the device and which calls Volumio makes into it.
| Type | For | Examples |
|---|---|---|
music_service | A source of music the user browses and searches | A streaming service, a web radio directory |
audio_interface | An audio input or output the user does not browse | AirPlay, Bluetooth, UPnP renderer, a DSP |
system_hardware | Hardware connected to the device | Buttons, LEDs, a rotary encoder, an amplifier relay, a remote |
user_interface | A way to see or control Volumio from outside the web UI | A display, an on-screen UI, a REST bridge |
system_controller | Parts of the system itself | Networking, storage, updates |
miscellanea | Anything that fits no other type | A scrobbler, a scheduler |
Only a music_service appears in Browse and gets Browse, play and search calls. Build a music service covers them.
The lifecycle
Volumio calls these methods in this order. Each one returns a kew promise, except getConfigurationFiles.
| Method | Volumio calls it | The plugin |
|---|---|---|
constructor(context) | When it loads an enabled plugin: at boot, or when the user enables it | Keeps context. Does no work. |
getConfigurationFiles() | Before onVolumioStart | Returns ['config.json']. Volumio copies that file to the configuration folder the first time. |
onVolumioStart() | Right after loading | Loads its saved settings. |
onStart() | After onVolumioStart | Starts its work. |
onStop() | When the user disables the plugin | Undoes everything onStart did. |
getUIConfig() | When the user opens the plugin's settings page | Returns the settings page. |
At boot, Volumio waits at most 30 seconds for all plugins to resolve onVolumioStart, then the same for onStart. After that it goes on without the slow ones. So a plugin resolves quickly and does long work afterwards.
The plugin acts only between onStart and onStop. Every timer, socket, child process and Browse entry that onStart creates, onStop removes.
index.js methods lists every method with its arguments.
Kew promises
Volumio uses kew, an older promise library, imported as libQ. Every method Volumio calls returns a kew promise:
const libQ = require('kew');
onStart() {
return libQ.resolve();
}
Volumio checks the type of what each method returns. An async method or a native Promise fails that check, and the log says does not return adequate promise. An onStart that fails it leaves the plugin failed. Volumio also chains .fail on what methods return, and a native Promise has no .fail.
Inside the plugin, async functions are fine. Where Volumio calls in, turn their result into a kew promise:
onStart() {
const defer = libQ.defer();
this.connect().then((value) => defer.resolve(value), (error) => defer.reject(error));
return defer.promise;
}
Call defer.resolve and defer.reject on defer, as above. Passed on their own, as .then(defer.resolve), they lose defer and the promise never settles.
Talking to Volumio
context.coreCommand is Volumio's command router. The template keeps it as this.commandRouter, and context.logger as this.logger. Every call into Volumio goes through the command router: a toast, the user's language, an entry in Browse, a call to another plugin.
this.commandRouter.pushToastMessage('success', 'Hello Volumio', 'Settings saved');
A plugin never replaces a command router method and never loads Volumio's own files with require. Both change Volumio for every other plugin. index.js methods lists the calls a plugin can make.
Settings
A plugin keeps its settings in config.json, one entry per setting with its type and default value. The first time the plugin runs, Volumio copies the file to /data/configuration/<type>/<id>/config.json. The plugin then reads and writes that copy, so the user's values survive updates. Add a settings page shows how the user changes them.
Where a plugin lives on the device
| What | Where |
|---|---|
| The plugin's files | /data/plugins/<type>/<id>/ |
| Its saved settings | /data/configuration/<type>/<id>/config.json |
| Node.js | Version 20.5, CommonJS modules (require) |
<type> and <id> are the type and id fields of volumio-plugin.json.
What a plugin is not
- A plugin is not a separate process. It runs inside Volumio's backend, so a crash or a blocking call in the plugin stops the whole player.
- A plugin does not change Volumio. It adds files and packages of its own and never edits what ships in the Volumio image.
- A plugin does not start long-running programs itself. A program that keeps running is a systemd unit the plugin installs.