Skip to main content

index.js methods

BetaThe plugin SDK and these pages are new. They change with each release, as developers use them and tell us what works.View as Markdown

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. 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​

MethodRequiredVolumio calls itReturns
constructor(context)YesWhen it loads an enabled pluginThe instance
getConfigurationFiles()YesBefore onVolumioStartAn array of file names
onVolumioStart()YesRight after loadingkew promise
onStart()YesAfter onVolumioStartkew promise
onStop()YesWhen the user disables the pluginkew promise
getUIConfig()With a settings pageWhen the user opens the settings pagekew promise of the page
The onSave methodNoWhen the user selects Save on the settings pagekew promise
onVolumioShutdown()NoBefore the device powers offkew promise
onVolumioReboot()NoBefore the device rebootskew promise

constructor(context)​

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

PropertyHolds
context.coreCommandThe command router. The template keeps it as this.commandRouter.
context.loggerThe Volumio logger. The template keeps it as this.logger.
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.

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.

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.

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.

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:

CallDoesReturns
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 openModalkew promise
sharedVars.get('language_code')The user's language, such as enstring
pluginManager.getConfigurationFile(context, name)The path of a configuration file on the devicestring
i18nJson(languageFile, englishFile, jsonFile)jsonFile with its TRANSLATE. keys translated, English for missing keyskew promise
executeOnPlugin(type, id, method, data)Calls a method of another pluginWhat 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:

CallDoesSee
addDSPSignalPathElement(element)Reports the plugin as an element of the signal pathReport DSP elements
removeDSPSignalPathElement({ id })Removes that elementReport DSP elements
registerThirdPartyUI({ uiPrettyName, uiName, uiPath })Adds a user interface users can pickAdd your 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.

Was this page helpful?