---
url: >-
  https://game-engine-io.github.io/enginex-frontend-sdk/reference/api/@getenginex/sdk-vue/functions/useRealtime.md
---
[Reference](../../../index.md) / [@getenginex/sdk-vue](../index.md) / useRealtime

# useRealtime()

> **useRealtime**<`T`>(`topic`, `handler`): `void`

Subscribes to `topic` while the scope (component or `effectScope`) exists and cancels when it ends. The
latest handler receives the messages, without resubscribing when it changes. The topic is compared by
key, not by object, so a getter that recreates `coreTopics.balance()` 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 a `null` topic
(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 component: it already goes to the client's `onError`. On the server (SSR) it subscribes to nothing.

## Type Parameters

### T

`T`

## Parameters

### topic

`MaybeRefOrGetter`<[`Topic`](../../sdk/extension/interfaces/Topic.md)<`T`> | `null`>

Topic descriptor, or `null`; accepts a ref or getter.

### handler

`Ref`<(`message`, `meta`) => `void`, (`message`, `meta`) => `void`> | ((`message`, `meta`) => `void`)

Called on every message, with the metadata (`kind`, `seq`, `topic`); accepts a function ref.

## Returns

`void`

## Throws

If called in an app without the `EnginexPlugin`.

## Example

```ts
useRealtime(coreTopics.balance(), (update) => { available.value = update.available })

// Product topic, only once the market is known:
useRealtime(() => (marketId.value ? predictionsTopics.orderbook({ marketId: marketId.value }) : null), apply)
```
