---
url: >-
  https://game-engine-io.github.io/enginex-frontend-sdk/reference/api/@getenginex/sdk-react/functions/useRealtimeSync.md
---
[Reference](../../../index.md) / [@getenginex/sdk-react](../index.md) / 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](../interfaces/RealtimeSyncOptions.md)); applying their message content (orderbook, orders) is left to the
app, via [useRealtime](useRealtime.md).

## Parameters

### options?

[`RealtimeSyncOptions`](../interfaces/RealtimeSyncOptions.md) = `{}`

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],
    },
  ],
})
```
