dart_nats_client 0.1.12 dart_nats_client: ^0.1.12 copied to clipboard
A Dart client for the NATS messaging system. Design to use with Dart and flutter. Forked from https://github.com/chartchuo/dart-nats
Dart NATS Client #
A Dart client for the NATS messaging system. Design to use with Dart and flutter.
Dart Examples #
All examples can be found in example
folder.
Simple poll example #
Run with:
dart example/simple_sub_pub.dart
import 'package:dart_nats_client/dart_nats_client.dart';
void main() async {
// Create client instance
var client = Client();
// Connect to server
await client.connect('localhost');
// Subscribe on topic
var sub = client.sub('subject1');
// Publish string to topic
client.pubString('subject1', 'message1');
// Wait message from topic
var msg = await sub.poll();
// Print received message
print(msg.string);
// Unsubscribe from topic
sub.unSub();
// Close client connection
client.close();
}
Listener example #
Run with:
dart example/simple_listener.dart
import 'package:dart_nats_client/dart_nats_client.dart';
void main() async {
// Create client instance
var client = Client();
// Connect to server
await client.connect('localhost');
// Subscribe on topic
var sub = client.sub('subject1');
// Subscribe on topic
var subListener = sub.getStream().listen((msg) {
print(msg.string);
});
// Publish string to topic
client.pubString('subject1', 'message1');
// Some delay for receiving
await Future.delayed(Duration(milliseconds: 100));
// Cancel listener
await subListener.cancel();
// Unsubscribe from topic
sub.unSub();
// Close client connection
client.close();
}
Flutter Example #
You can find it in example/simple_listener.dart.flutter
For run you need to init Flutter project in your folder with flutter create .
and copy text from example file to lib/main.dart
App permissions #
Android permissions
For android you need to add to android/app/src/profile/AndroidManifest
file lines:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
For activiy in backgroud:
<!-- Allows run app in background -->
<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND"/>
<!-- Allows app use data in background -->
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND"/>
iOS permissions
For iOS you don't need any specific permissions for NATS client.
But for background activity you need to add to file ios/Runner/Info.plist
lines:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>external-accessory</string>
<string>fetch</string>
<string>processing</string>
<string>remote-notification</string>
</array>
Testing #
For running unit-tests use dart test
in project root folder.
NOTE. For testing you need run NATS in docker. Instruction
Features #
The following is a list of features currently supported and planned by this client:
-
- Publish
-
- Subscribe, unsubscribe
-
- NUID, Inbox
-
- Reconnect to single server when connection lost and resume subscription
-
- Unsubscribe after N message
-
- Request, Respond
-
- Respond, Request example
-
- Queue subscribe
-
- caches, flush, drain
-
- Request timeout
-
- structured data
-
- Connection option (cluster, timeout,ping interval, max ping, echo,... )
-
- Random automatic reconnection, disable reconnect, number of attempts, pausing
-
- Connect to cluster,randomize, Automatic reconnect upon connection failure base server info
-
- Events/status disconnect handler, reconnect handler
-
- Buffering message during reconnect atempts
-
- All authentication models, including NATS 2.0 JWT and seed keys
-
- TLS support