Skip to content

Search v2 documentation only

Types

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
}
interface SendMessageOptions {
expirationDuration?: number; // ms until the message expires
expirationDate?: number; // absolute expiry timestamp
}
interface ClearReceivedMessagesOptions {
ids?: string[]; // clear specific messages
types?: string[]; // clear by message type
sources?: string[]; // clear by source name
}
interface ClearSentMessagesOptions {
ids?: string[];
types?: string[];
sync?: boolean; // also clear other tabs' received copies
}
interface GetLatestMessageOptions {
type?: string;
source?: string;
}

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
}
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.

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.