useDailyEvent and useThrottledDailyEvent register and tear down listeners for you.
Reach for a hook first
Before subscribing to an event, check whether a hook already exposes the state it carries. The hooks keep the state for you and re-render on change, which is usually what you actually want:
Many of these hooks also accept the corresponding
on... callback, so you can run side effects on the event while still getting the reactive state.
Subscribe with useDailyEvent
For events without a dedicated hook,useDailyEvent registers a listener and removes it when the component unmounts:
The callback must be memoized
This is the one rule that trips people up. Thecallback you pass must be a stable reference, normally wrapped in useCallback:
Batch high-frequency events
Some events fire in bursts, for example a wave ofparticipant-joined events when a large call starts. useThrottledDailyEvent collects events over a window and hands you an array of everything that happened, so you update state once instead of once per event:
throttleTimeout to change it. The same memoized-callback rule applies. Note that the callback receives an array, unlike useDailyEvent, which receives a single event.
Choosing between them
- One-off or low-frequency event, react immediately:
useDailyEvent. - Bursty or high-frequency event where you only need the net result:
useThrottledDailyEvent. - The event maps to call state you want to render: use the dedicated hook from the table above.
Next steps
Custom messages
Send and receive your own data with useAppMessage.
Events reference
Every daily-js event and its payload.