> 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/getting-started/first-message/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

Send your first **1:1 realtime text message** after connecting over WebSocket. This example assumes you completed [**First connection**](https://dnotifier.gitbook.io/product-docs/getting-started/first-connection/dart-flutter.md).

## Complete example

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

const myUserId = 'user-alice';
const peerUserId = 'user-bob';

Future<void> main() async {
  final notifier = DNotifier(
    appId: 'your-app-id',
    secret: 'your-app-secret',
    transport: 'ws',
    userId: myUserId,
    onConnected: () async {
      print('Connected as $myUserId');

      await notifier.send(
        senderId: myUserId,
        receiverId: peerUserId,
        data: {
          'type': 'text',
          'text': 'Hello from DNotifier!',
        },
      );

      print('Message sent to $peerUserId');
    },
    onMessage: (DNotifierMessage msg) {
      print('[${msg.metadata.sender}] ${msg.payload.toJSON()}');
    },
    onDisconnected: ({code, reason}) {
      print('Disconnected: $reason');
    },
  );

  await notifier.connect();
}
```

Replace credentials with your dashboard values or compile-time defines.

Run:

```bash
dart run bin/send_first_message.dart
```

## Send after connect (decoupled)

```dart
await notifier.connect();

await notifier.send(
  senderId: 'user-alice',
  receiverId: 'user-bob',
  data: {
    'type': 'text',
    'text': 'Hello from DNotifier!',
  },
  saveHistory: true, // default — set false to skip chat history persistence
);
```

## Flutter button example

```dart
ElevatedButton(
  onPressed: () async {
    if (!notifier.isConnected) return;
    await notifier.send(
      senderId: myUserId,
      receiverId: peerUserId,
      data: {'type': 'text', 'text': 'Hello from Flutter!'},
    );
  },
  child: const Text('Send message'),
)
```

## Echo test (two processes)

**Process 1 — bob listens:**

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

Future<void> main() async {
  final bob = DNotifier(
    appId: 'your-app-id',
    secret: 'your-app-secret',
    transport: 'ws',
    userId: 'user-bob',
    onConnected: () => print('Bob is online'),
    onMessage: (msg) => print('Bob received: ${msg.payload.toJSON()}'),
  );
  await bob.connect();
}
```

**Process 2 — alice sends:** use the complete example at the top of this page.

Expected on bob's console:

```
Bob received: {type: text, text: Hello from DNotifier!}
```

## Troubleshooting

| Issue                                               | Check                                           |
| --------------------------------------------------- | ----------------------------------------------- |
| `StateError: Not connected`                         | Await `connect()` before `send()`               |
| Receiver gets nothing                               | Receiver must use same `appId` and be connected |
| `ArgumentError: receiverId or receiverIds required` | Pass `receiverId` or `receiverIds`              |
| Auth OK but no delivery                             | Use `transport: 'ws'`, not `'http'`             |

## Learn more

* [**Understanding messages**](https://dnotifier.gitbook.io/product-docs/getting-started/understanding-messages.md)
* [**Send & receive text — Dart / Flutter**](https://dnotifier.gitbook.io/product-docs/realtime-communication/send-and-receive/dart-flutter.md)
