# Realtime updates

Canonical page: https://docs.ovok.com/flutter-sdk/realtime

`ovok.realtime.subscribe()` creates a FHIR `Subscription`, opens its authenticated
WebSocket, and calls your handler for each matching resource change. The criteria
uses the FHIR subscription search format.

```dart
final stop = await ovok.realtime.subscribe(
  'Observation?subject=Patient/$patientId',
  handleNotification,
);
```

The handler receives `SubscriptionStatus` event notifications.
`notification['resources']` holds the changed resources the server sent with
each one, in server order. Handshake, heartbeat and ping/pong messages stay
internal.

```dart
void handleNotification(RealtimeNotification notification) {
  final resources = notification['resources'] as List<Map<String, dynamic>>;
  // Update your state from the changed resources.
}
```

Use the notification to update your state or refresh the affected view. Keep
FHIR payloads out of application logs.

Keep the returned function while the screen or service needs updates, then call
it to close the socket and delete the server-side Subscription:

```dart
await stop();
```

The client reconnects after a dropped connection, refreshes its short-lived
WebSocket binding, and pauses while signed out. It resumes when the same profile
signs in again. Changing profiles ends the subscription. A blank criteria string
throws `OvokValidationException`.
