> 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/realtime-communication/binary-messaging/dart-flutter.md).

# Dart / Flutter

Send raw bytes with `sendBinary()`.

***

## Basic example

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

Future<void> main() async {
  final notifier = DNotifier(
    appId: 'your-app-id',
    secret: 'your-app-secret',
    transport: 'ws',
    userId: 'user-alice',
    onConnected: () {},
    onMessage: (msg) {
      if (msg.metadata.type == 'binary') {
        final bytes = msg.payload.raw();
        print('Binary frame: ${bytes.length} bytes');
        return;
      }
      print(msg.payload.toJSON());
    },
  );

  await notifier.connect();

  notifier.sendBinary(
    senderId: 'user-alice',
    receiverIds: ['user-bob'],
    buffer: [0x00, 0x01, 0x02, 0xff],
  );
}
```

***

## Multiple receivers

```dart
notifier.sendBinary(
  senderId: 'user-alice',
  receiverIds: ['user-bob', 'user-carol'],
  buffer: compressedStateBytes,
);
```

***

## Custom protocol example

```dart
import 'dart:typed_data';

Uint8List encodeCursorOp(double x, double y) {
  final buffer = ByteData(8);
  buffer.setFloat32(0, x);
  buffer.setFloat32(4, y);
  return buffer.buffer.asUint8List();
}

notifier.sendBinary(
  senderId: myUserId,
  receiverIds: [peerUserId],
  buffer: encodeCursorOp(120.5, 340.0).toList(),
);
```

Receiver:

```dart
onMessage: (msg) {
  final raw = msg.payload.raw();
  final buffer = ByteData.sublistView(Uint8List.fromList(raw));
  final x = buffer.getFloat32(0);
  final y = buffer.getFloat32(4);
  updatePeerCursor(x, y);
},
```

***

## Flutter note

For chat images and files, prefer `send()` with structured `data` — it integrates better with history and UI rendering. Reserve `sendBinary()` for custom sync protocols.

***

## Troubleshooting

| Issue                                | Check                                       |
| ------------------------------------ | ------------------------------------------- |
| Receiver gets JSON instead of binary | Confirm you called `sendBinary`, not `send` |
| `ArgumentError` on receiverIds       | Pass a non-empty list                       |
| Garbled bytes on receive             | Use `payload.raw()`, not `toJSON()`         |

***

## Learn more

* [Structured payloads](https://dnotifier.gitbook.io/product-docs/realtime-communication/structured-payloads/dart-flutter.md)
* [Large messages & chunking](https://dnotifier.gitbook.io/product-docs/realtime-communication/chunking.md)
