This MigratoryData client library is written in Dart. One important use of this library is that it allows you to use the Flutter framework for building realtime applications natively compiled for mobile (Android and iOS), web, and desktop from a single codebase.
Features
The API exposed by MigratoryData client library for Dart is the same as the API exposed by the MigratoryData client libraries for all other supported programming languages such as JavaScript, Java, C++, C#, Python, PHP, Objective-C, etc. The API contains all the necessary operations for:
- connecting to a cluster of one or more instances of MigratoryData Server version 6.x,
- subscribing to one or more subjects,
- getting real-time messages for the subscribed subjects, and
- publishing real-time messages
Getting started
MigratoryData uses the standard pub
tool for package management to make available MigratoryData client library for Dart.
To add the package to your Dart or Flutter application, add MigratoryData client library as a dependency in your pubspec.yaml
as follows:
dependencies:
migratorydata_client_dart_v6: ^1.0.2
Then, change to the directory of your application (the directory which contains pubspec.yaml
) and run the following command:
dart pub get
Usage
Copy the code below to a file named client_dart_example.dart
and run it using the following command:
dart client_dart_example.dart
Supposing the MigratoryData server version 6.x is installed locally on your machine, using the default configuration. This example client application connects to the MigratoryData server deployed locally at localhost:8800
, subscribes to a subject /vehicles/1/speed
, publishes to the MigratoryData server a message every 5 seconds (containing a random speed between 0
and 200
) on the same subject, and receives from the MigratoryData server the published message in realtime.
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'dart:math';
import 'package:migratorydata_client_dart_v6/client.dart';
void main() {
var client = MigratoryDataClient();
client.setEntitlementToken("some-token");
client.setListener(ListenerImpl());
// client.setEncryption(true);
client.setServers(["localhost:8800"]);
client.subscribe(["/vehicles/1/speed"]);
client.connect();
Timer.periodic(Duration(seconds: 5), (Timer t) {
client.publish(MigratoryDataMessage(
"/vehicles/1/speed",
Uint8List.fromList(utf8.encode(Random().nextInt(200).toString()))));
});
}
class ListenerImpl implements MigratoryDataListener {
@override
onMessage(MigratoryDataMessage message) {
print(message);
}
@override
onStatus(String type, String info) {
print("$type - $info");
}
}