Skip to main content

Plugin Events

In most cases, a plugin method performs a task and finishes when its Task completes. But there are situations where the native side must push updates to the web side repeatedly. Napacitor handles this with an event pipeline — NotifyListeners on the C# side paired with addListener on the JS side.

This replaces Capacitor's saveCall/keepAlive machinery. There is nothing to save: a single async completion is expressed by the method's Task, and repeated updates are expressed as events.

Overview​

Two reasons a plugin might need to talk back to JS after the method returns:

  1. To perform an asynchronous action (a network request, a permission prompt);
  2. To provide repeated updates, such as streaming battery level changes or geolocation data.

For (1), write an async method and return the result — the bridge matches the response to the originating JS promise automatically. For (2), use the event pipeline.

The event pipeline​

[NapacitorPlugin("Battery")]
public class BatteryPlugin : NapacitorPluginBase
{
[NapacitorMethod]
public Task<BatteryInfo> GetBatteryInfo() { … }

protected override void OnListenerAdded(string eventName)
{
// First JS listener arrived — subscribe to the native source.
Battery.BatteryInfoChanged += OnBatteryInfoChanged;
}

protected override void OnListenerRemoved(string eventName)
{
// Last JS listener left — unsubscribe from the native source.
Battery.BatteryInfoChanged -= OnBatteryInfoChanged;
}

private void OnBatteryInfoChanged(object? sender, BatteryInfoChangedEventArgs e)
{
_ = NotifyListeners("batteryStatusChange", new BatteryInfo
{
BatteryLevel = e.ChargeLevel,
IsCharging = e.State == BatteryState.Charging,
});
}
}

The runtime tracks listeners per (plugin, eventName) through the callback IDs registered from JS, so NotifyListeners only reaches the web side while at least one listener is attached.

JS side:

import { Battery } from '@napacitor/battery';

const handle = await Battery.addListener('batteryStatusChange', (info) => {
console.log(info.batteryLevel, info.isCharging);
});

// stop receiving events
await handle.remove();

addListener returns a PluginListenerHandle; remove() unregisters it, and removeAllListeners() clears the plugin's listeners.

Event data​

Event payloads go through the same serialization as call results — camelCase JSON, case-insensitive matching (see Data Types). JS receives the parsed object as the callback argument.

Broadcasting to window/document​

Plugin events are scoped to listeners registered through addListener. To reach any JS code via a DOM event instead (for example, for a download progress bar), use TriggerJSEventAsync — see NapacitorRuntime.

Listener lifecycle summary​

EventTriggerPurpose
OnListenerAdded(eventName)first JS listener for that eventsubscribe to native source
NotifyListeners(eventName, data)native update availablepush JSON to all JS listeners
OnListenerRemoved(eventName)last JS listener for that event removedunsubscribe from native source

The web implementations follow the same addListener contract, so plugin event code works unchanged in the browser.