useBroadcastChannel
const actions = useBroadcastChannel(channelName, options?);// or, with a typed message map (2.5.0+):const typedActions = useBroadcastChannel<DashboardEvents>(channelName, options?);| Parameter | Type | Required | Description |
|---|---|---|---|
channelName |
string |
yes | Channel name. Combined with options.namespace when set. |
options |
BroadcastOptions |
no | See below. |
Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
sourceName |
string |
generated | Name attached to every message this tab sends. Keep it unique per tab; a tab ignores messages whose source matches its own name. |
namespace |
string |
none | Prefixes the channel name to isolate modules or environments. |
registeredTypes |
string[] |
[] (all) |
Message types to accept. Others are dropped before state and callbacks. |
keepLatestMessage |
boolean |
false |
Keep only the single most recent message (one slot across all types). |
onMessage |
MessageCallback | OnMessageMap |
none | Callback(s) fired once per accepted incoming message. A single function, or a type-keyed map. |
cleaningInterval |
number (ms) |
1000 |
How often expired messages are swept. |
cleanupDebounceMs |
number (ms) |
0 |
Debounce for cleanup. Keep below cleaningInterval. |
deduplicationTTL |
number (ms) |
5 minutes | How long a seen message ID blocks redelivery. |
batchingDelayMs |
number (ms) |
20 |
Window for batching outgoing messages. 0 disables batching. |
excludedBatchMessageTypes |
string[] |
[] |
Types that always send immediately. |
recovery |
'auto' | 'manual' |
'auto' |
Automatic recovery on provable channel death: recreates the channel and retries that send once. 'manual' opts out. 2.4.0+ |
maxRecoveryAttempts |
number |
3 |
Consecutive failed recoveries before the channel stays down until an explicit reconnect(). 2.4.0+ |
onChannelRecovered |
() => void |
none | Fired after a successful automatic recovery; use it to resync. Missed messages are never replayed. 2.4.0+ |
telemetry |
boolean |
false |
Deprecated no-op. The library collects no telemetry (details). |
Typed channels (2.5.0+)
Section titled “Typed channels (2.5.0+)”Pass an optional message map as the generic to type postMessage, onMessage, getLatestMessage, the types filters of the clear/history functions, and registeredTypes / excludedBatchMessageTypes:
type DashboardEvents = { filter: { range: string; team: string }; logout: undefined;};
const { postMessage } = useBroadcastChannel<DashboardEvents>('dashboard');
postMessage('filter', { range: '7d', team: 'a' }); // okpostMessage('logout'); // ok: undefined payload, second argument optionalpostMessage('flter', {}); // compile error: unknown message typeTypes only: no runtime validation, no wire-format change. Omitting the generic keeps the loose string / any behavior. See the Typed Channels recipe.
Returns
Section titled “Returns”A BroadcastActions object: messages, sentMessages, postMessage, ping, isPingInProgress, getLatestMessage, clearReceivedMessages, clearSentMessages, closeChannel, requestMessagesHistory (2.3.0+), channelName, and error.
Example
Section titled “Example”const { messages, postMessage, error } = useBroadcastChannel('notifications', { namespace: 'admin', registeredTypes: ['alert', 'info'], keepLatestMessage: false, deduplicationTTL: 60_000, batchingDelayMs: 50, excludedBatchMessageTypes: ['alert'], onMessage: { alert: msg => toast(msg.message) },});