Flutter and Dart

The byotalk Dart package brings chat to Flutter apps (Android, iOS, web, desktop) and to plain Dart. It speaks the same realtime protocol as the JavaScript SDK: messages, sync after reconnect, an offline outbox, read receipts, typing and presence. Voice and video calls are not in the Dart SDK yet; use the web or React Native SDK for those.

1. Install

dependencies:
  byotalk:
    git:
      url: https://github.com/devicorn/byotalk-sdk.git
      path: flutter

The package is pure Dart and depends only on http and web_socket_channel. Once it is on pub.dev this becomes byotalk: ^0.1.0.

2. Connect and send (development)

Development environments accept dev tokens, so you can start without a backend.

import 'package:byotalk/byotalk.dart';

final chat = ByoTalkChat(env: 'env_dev_2f8k', token: ByoTalkChat.devToken('alice'));
await chat.connect();
final dm = await chat.conversations.direct('bob');
dm.events.listen((e) {
  if (e is MessageNew) print('${e.message.senderId}: ${e.message.text}');
});
await dm.send(text: 'Hello Bob');

Users are created on first connect.

3. Production

Dev tokens are always disabled in production. Add the token route from the Quick start to your backend, then pass a function that fetches a token. It is called again before the token expires.

final chat = ByoTalkChat(
  env: 'env_2f8k',
  token: () async {
    final res = await http.get(Uri.parse('https://your.app/chat-token'), headers: {'authorization': 'Bearer $session'});
    return jsonDecode(res.body)['token'] as String;
  },
);

Never ship your secret key in an app: ByoTalkChat refuses tokens that start with sk_.

4. Show messages

conversation.messages.items is the ordered message window (oldest first, your unsent messages last) and conversation.messages.changes emits a new list after every change, which fits a StreamBuilder:

StreamBuilder<List<Message>>(
  stream: conversation.messages.changes,
  initialData: conversation.messages.items,
  builder: (context, snap) => ListView(
    children: [for (final m in snap.data!) Text('${m.senderId}: ${m.text ?? ''}')],
  ),
);

Load older pages with await conversation.loadOlder() while conversation.hasOlder is true. Call conversation.typing() on every keystroke and conversation.markRead() when the conversation is on screen. chat.conversations.watch() is a live list with unread counts and last messages for your inbox screen.

5. App lifecycle

Disconnect when the app goes to the background and connect again on resume. The server marks the user offline at once, push notifications take over, and connect() syncs what was missed.

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  if (state == AppLifecycleState.paused) chat.disconnect();
  if (state == AppLifecycleState.resumed) chat.connect();
}

Messages sent while offline stay pending and are sent after the next connect with the same id, so they are never duplicated. To keep them across an app restart, pass a persistence that implements get, set and delete (for example on top of shared_preferences; the package README has a ready-made class). On sign-out call await chat.clearLocalData() then await chat.disconnect().

6. Errors

Every failure is a ChatException with code, message and requestId (quote the request id when you contact support). Connection problems never fail a send: the connection state goes to reconnecting (see chat.connectionStates) and the SDK retries with backoff. A refused token or a suspended environment moves the state to failed and is reported on chat.errors.