> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/examples/hello-realtime/dart-flutter.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Dart / Flutter

## Complete example (Dart CLI)

Save as `bin/hello_realtime.dart`:

```dart
import 'package:dnotifier/dnotifier.dart';

Future<void> main(List<String> args) async {
  const userId = String.fromEnvironment('DNOTIFIER_USER_ID', defaultValue: 'user-alice');
  const peerId = String.fromEnvironment('DNOTIFIER_PEER_ID', defaultValue: 'user-bob');

  final notifier = DNotifier(
    appId: const String.fromEnvironment('DNOTIFIER_APP_ID'),
    secret: const String.fromEnvironment('DNOTIFIER_SECRET'),
    transport: 'ws',
    userId: userId,
    onConnected: () async {
      print('[$userId] Connected');
      await notifier.send(
        senderId: userId,
        receiverId: peerId,
        data: {'type': 'text', 'text': 'Hello from DNotifier!'},
      );
      print('[$userId] Message sent');
    },
    onMessage: (msg) {
      print('[$userId] From ${msg.metadata.sender}: ${msg.payload.toJSON()}');
    },
    onDisconnected: ({code, reason}) {
      print('[$userId] Disconnected: $reason');
    },
  );

  await notifier.connect();

  // Keep alive for incoming messages
  await Future.delayed(const Duration(hours: 1));
}
```

## Run

```bash
dart run bin/hello_realtime.dart \
  --define=DNOTIFIER_APP_ID=your-app-id \
  --define=DNOTIFIER_SECRET=your-app-secret \
  --define=DNOTIFIER_USER_ID=user-alice \
  --define=DNOTIFIER_PEER_ID=user-bob
```

## Flutter

Wrap the same `DNotifier` setup in `initState()` of a `StatefulWidget`. Call `notifier.disconnect()` in `dispose()`.

## Expected output

```
[user-alice] Connected
[user-alice] Message sent
```

Run a second instance with `DNOTIFIER_USER_ID=user-bob` to receive the message.
