The Tauri event system is a multi-producer multi-consumer communication primitive that allows message passing between the frontend and the backend.
It is analogous to the command system, but payload type check must be written on the event handler and it simplifies communication from the backend to the frontend, working like a channel.
A Tauri application can listen and emit to global and window-specific events. Usage from the frontend and the backend are described below.
## Frontend
The event system is accessible on the frontend on the `event` and `window` modules of the `@tauri-apps/api` package.
### Global events
To use the global event channel, import the `event` module and use the `emit` and `listen` functions:
```ts
import { emit, listen } from '@tauri-apps/api/event'
// listen to the `click` event and get a function to remove the event listener
// there's also a `once` function that subscribes to an event and automatically unsubscribes the listener on the first event
const unlisten = await listen('click', event => {
// event.event is the event name (useful if you want to use a single callback fn for multiple event types)
// event.payload is the payload object
})
// emits the `click` event with the object payload
emit('click', {
theMessage: 'Tauri is awesome!'
})
```
### Window-specific events
Window-specific events are exposed on the `window` module.