Skip to main content

event

@tauri-apps/api / event

Module: event

The event system allows you to emit events to the backend and listen to events from it.

This package is also accessible with window.__TAURI__.event when build.withGlobalTauri in tauri.conf.json is set to true.

Interfaces​

Type Aliases​

EventCallback​

EventCallback<T>: (event: Event<T>) => void

Type parameters​

Name
T

Type declaration​

(event): void

Parameters​
NameType
eventEvent<T>
Returns​

void

Defined in​

helpers/event.ts:22


EventName​

EventName: string

Defined in​

helpers/event.ts:20


UnlistenFn​

UnlistenFn: () => void

Type declaration​

(): void

Returns​

void

Defined in​

helpers/event.ts:24

Functions​

emit​

emit(event, payload?): Promise<void>

Emits an event to the backend.

Example

import { emit } from '@tauri-apps/api/event';
await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });

Parameters​

NameTypeDescription
eventstringEvent name. Must include only alphanumeric characters, -, /, : and _.
payload?unknownEvent payload

Returns​

Promise<void>


listen​

listen<T>(event, handler): Promise<UnlistenFn>

Listen to an event from the backend.

Example

import { listen } from '@tauri-apps/api/event';
const unlisten = await listen<string>('error', (event) => {
console.log(`Got error in window ${event.windowLabel}, payload: ${payload}`);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Type parameters​

Name
T

Parameters​

NameTypeDescription
eventstringEvent name. Must include only alphanumeric characters, -, /, : and _.
handlerEventCallback<T>Event handler callback.

Returns​

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


once​

once<T>(event, handler): Promise<UnlistenFn>

Listen to an one-off event from the backend.

Example

import { once } from '@tauri-apps/api/event';
interface LoadedPayload {
loggedIn: boolean,
token: string
}
const unlisten = await once<LoadedPayload>('loaded', (event) => {
console.log(`App is loaded, logggedIn: ${event.payload.loggedIn}, token: ${event.payload.token}`);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Type parameters​

Name
T

Parameters​

NameTypeDescription
eventstringEvent name. Must include only alphanumeric characters, -, /, : and _.
handlerEventCallback<T>Event handler callback.

Returns​

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.