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 tauri.conf.json > build > withGlobalTauri 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:41


EventName

Ƭ EventName: LiteralUnion<"tauri://update" | "tauri://update-available" | "tauri://update-download-progress" | "tauri://update-install" | "tauri://update-status" | "tauri://resize" | "tauri://move" | "tauri://close-requested" | "tauri://focus" | "tauri://blur" | "tauri://scale-change" | "tauri://menu" | "tauri://file-drop" | "tauri://file-drop-hover" | "tauri://file-drop-cancelled" | "tauri://theme-changed", string>

Defined in

helpers/event.ts:21


UnlistenFn

Ƭ UnlistenFn: () => void

Type declaration

▸ (): void

Returns

void

Defined in

helpers/event.ts:43

Functions

emit

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

Emits an event to the backend.

example Emits the frontend-loaded event with the given payload

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?unknown-

Returns

Promise<void>

Defined in

event.ts:81


listen

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

Listen to an event from the backend.

example Listen to the error event expecting a string payload

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

// removes the listener later
await unlisten();

Type parameters

Name
T

Parameters

NameTypeDescription
eventEventNameEvent 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.

Defined in

event.ts:37


once

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

Listen to an one-off event from the backend.

example Listen to the loaded event that is only triggered once

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}`);
});

Type parameters

Name
T

Parameters

NameTypeDescription
eventEventNameEvent 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.

Defined in

event.ts:62