Skip to content

Event API ​

The event namespace provides cross-window event dispatching across WebView2 instances.

event ​

event.emit ​

Broadcast an event to every window. Receivers get an event named event whose data is { payload, sourceWindowId }.

ParameterTypeRequiredDescription
eventstringYesEvent name the receivers subscribe to. Must not be empty.
payloadanyNoData delivered as payload; absent or null sends an empty object.
excludeSelfbooleanNoLeave out the calling window. Default: false.

Returns

FieldTypeDescription
recipientsintegerHow many windows the event was sent to, counted from the open windows.

success is true on success. On failure the response is { success: false, error, code }; see Error codes for code.

js
await fb2k.invoke('event.emit', { event: 'ui:themeChanged', payload: { theme: 'dark' } });

event.emitTo ​

Send an event to one window. It receives an event named event whose data is { payload, sourceWindowId }. A window id no open window has fails with NOT_FOUND.

ParameterTypeRequiredDescription
eventstringYesEvent name the receiver subscribes to. Must not be empty.
targetWindowIdstringYesId of the window to send to, such as main or the windowId a port reported. Must not be empty.
payloadanyNoData delivered as payload; absent or null sends an empty object.

Returns

success is true on success. On failure the response is { success: false, error, code }; see Error codes for code.

js
await fb2k.invoke('event.emitTo', { event: 'lyrics:update', targetWindowId: 'popup_01', payload: { line: 5 } });

Event envelopes ​

  • event.emit broadcasts the requested event name, and event.emitTo targets a specific window. Receivers get the envelope { payload, sourceWindowId }; excludeSelf applies only to event.emit. Use the namespace:eventName convention for application-defined events, such as ui:themeChanged or lyrics:update.