Types
BroadcastMessage
Section titled “BroadcastMessage”interface BroadcastMessage { id: string; // unique message identifier type: string; // the label passed to postMessage message: any; // your payload timestamp: number; // creation time (ms) source: string; // sourceName of the sending tab expirationDate?: number; // set when sent with an expiry isHistory?: boolean; // merged by requestMessagesHistory, never fires onMessage}SendMessageOptions
Section titled “SendMessageOptions”interface SendMessageOptions { expirationDuration?: number; // ms until the message expires expirationDate?: number; // absolute expiry timestamp}ClearReceivedMessagesOptions
Section titled “ClearReceivedMessagesOptions”interface ClearReceivedMessagesOptions { ids?: string[]; // clear specific messages types?: string[]; // clear by message type sources?: string[]; // clear by source name}ClearSentMessagesOptions
Section titled “ClearSentMessagesOptions”interface ClearSentMessagesOptions { ids?: string[]; types?: string[]; sync?: boolean; // also clear other tabs' received copies}GetLatestMessageOptions
Section titled “GetLatestMessageOptions”interface GetLatestMessageOptions { type?: string; source?: string;}RequestMessagesHistoryOptions
Section titled “RequestMessagesHistoryOptions”Available in 2.3.0 and later. Options for requestMessagesHistory.
interface RequestMessagesHistoryOptions { types?: string[]; // only request these message types (default: all) limit?: number; // max messages kept per request, default 50, max 500}MessageCallback and OnMessageMap
Section titled “MessageCallback and OnMessageMap”type MessageCallback = (msg: BroadcastMessage) => void;type OnMessageMap = { [type: string]: MessageCallback };onMessage accepts either a single callback for all incoming messages or a type-keyed map. It fires after the message is added to state and only for messages passing every filter. Internal messages (PING, PONG, CLEAR_SENT_MESSAGES) never trigger it.
Typed channel types (2.5.0+)
Section titled “Typed channel types (2.5.0+)”Helpers for typed channels. All erased at compile time.
// A channel's declared message map: message type -> payload type.type BroadcastMessageMap = Record<string, any>;
// A message narrowed to a map; distributes over TType into a discriminated union.type TypedBroadcastMessage<TMap, TType = keyof TMap> = ...;
// postMessage typed against the map; payload optional for undefined-payload types.type TypedPostMessage<TMap> = ...;
// onMessage map / single callback typed against the map.type TypedOnMessageMap<TMap> = { [K in keyof TMap]?: (msg: TypedBroadcastMessage<TMap, K>) => void };type TypedMessageCallback<TMap> = (msg: TypedBroadcastMessage<TMap>) => void;
// getLatestMessage returning the payload that matches the requested type.type TypedGetLatestMessage<TMap> = ...;BroadcastOptions<TMap>, BroadcastActions<TMap>, BroadcastProviderProps<TMap> and the clear/history option types all accept the map as an optional generic that defaults to the loose behavior.
