Reference / @getenginex/sdk-react / useRealtime
useRealtime()
useRealtime<
T>(topic,handler):void
Subscribes to topic on mount and cancels on unmount. Safe in StrictMode (mount, unmount, mount ends with a single subscription) and without resubscribing when handler changes: the latest handler receives the messages. The topic is compared by key, not by object, so coreTopics.balance() on every render does not resubscribe; a different topic does resubscribe.
A private topic (coreTopics.balance()) is only subscribed with a user in the session and resubscribes when the user changes: the handler never receives messages from the previous user. With topic: null (e.g. a market id not yet known) it does not subscribe. Without WebSocket available nothing happens (the status stays 'unavailable'). An error from the subscription itself (a disposed client, for example) does not take down the React tree: it already goes to the client's onError.
Type Parameters
T
T
Parameters
topic
Topic<T> | null
Topic descriptor (coreTopics.balance(), predictionsTopics.orderbook({ marketId })), or null.
handler
(message, meta) => void
Called on every message, with the metadata (kind, seq, topic).
Returns
void
Throws
If called outside an EnginexProvider.
Example
useRealtime(coreTopics.balance(), (update) => setAvailable(update.available))
// Product topic, only once the market is known:
useRealtime(marketId ? predictionsTopics.orderbook({ marketId }) : null, (book, meta) => apply(book, meta.kind))