Handy TDLib
This plugin is an updated fork of i-Naji's TDLib plugin with Dart 3 features and new TDLib version
About plugin
This is a Flutter plugin for TDLib (Telegram Database Library) v1.8.35
This plugin is a complete TDLib's JSON interface binding package to help you create your own Telegram client.
Installation
Install the plugin by running the following command from the project root:
flutter pub add handy_tdlib
Initialization
This plugin supports only Android.
Consider running this plugin isolated from the main thread. Do convertJsonToObject()
on separate thread to avoid any freezes.
Setting up
-
Set up 2 isolates with the working
SendPort
+ReceivePort
pair. -
Get TDLib client ID with
TdPlugin.instance.tdCreateClientId()
on invokes thread. -
Transfer this ID to updates thread.
-
Set up
await for
on invokes thread forReceivePort
.Run
TdPlugin.instance.tdSend
on every event fromReceivePort
. This will send all updates to TDLib. Consider running jsonEncode on this thread to improve UI performance.On the main thread, you should use `SendPort' with TdFunction to send invokes.
Hey, what about
extra
argument inTdFunction.toJson
?You should put here any random number and save it somewhere in your class instance. This is used as a sign to detect what invoke's result have you got from updates thread. Consider using
Timer
to make timeouts for invokes. -
On updates thread, set up
while (true) { ... }
with something like thisfinal String? response = TdPlugin.instance.tdReceive(clientId); if (response != null) { try { sendPort.send(convertJsonToObject(response)); } catch (e) { ... some logging of this exception ... } }
On UI thread, you'll get updates and invoke results from ReceivePort of this isolate.
Questions?
Hey, but how we'll separate invoke results from updates?
// Only invoke results have 'extra' field.
final bool isUpdate = object.extra == null;
Library uses sealed classes, so you can use Dart 3's new switch (...) { ... }
on object type if needed.
Consider importing lib/api.dart with prefix (import '...' as ...
). Some object names are the same with dart:io
and Flutter.
Still have questions?
Feel free to PM me in Telegram.
Check HandyGram on GitHub for some code examples.
Usage
Documentation button on this pub page
Official TDLib guide (a bit old)
Documentation for currently used by this plugin TDLib version
Libraries
- api
- client
- handy_tdlib
- Handy TDLib library