This version (v0.7) has not been published to npm yet.
Skip to content

Reference / @getenginex/sdk-react / useRealtimeSync

useRealtimeSync() ​

useRealtimeSync(options?): void

Connects realtime to the TanStack Query cache (opt-in). Subscribes to coreTopics.balance() and coreTopics.notification() while there is a user in the session:

  • on balance_update, invalidates the current user's balance query (wallet.getBalance); it never writes to the cache with setQueryData, because the message is incomplete (reserved, bonus and total arrive zeroed). The query refetches the full balance over HTTP;
  • on each notification, invalidates the user's notifications queries;
  • on a topic's onResync (after a reconnection, when messages may have been lost), invalidates what the topic feeds: balance and other wallet queries (balance) or notifications (notification).

Invalidation refreshes only active queries; inactive ones are refetched when they are used again. Product topics come in through options.topics, each with the query keys it feeds (see RealtimeSyncOptions); applying their message content (orderbook, orders) is left to the app, via useRealtime.

Parameters ​

options? ​

RealtimeSyncOptions = {}

Extra topics.

Returns ​

void

Throws ​

If called outside an EnginexProvider.

Example ​

ts
function Shell({ children }: { children: ReactNode }) {
  useRealtimeSync()
  return children
}

// With a product topic: each message and the orderbook resync invalidate the market's orderbook.
const pq = useEnginexQueries(createPredictionsQueries, predictions)
useRealtimeSync({
  topics: [
    {
      topic: predictionsTopics.orderbook({ marketId }),
      invalidate: [pq.markets.getOrderbook({ marketId }).queryKey],
    },
  ],
})