0 purchases
dart nats streaming
Dart-NATS-Streaming #
A null-safety Dart client for the NATS Streaming messaging system. Design to use with Dart and flutter.
This project is still in pre-release and not all features are yet implemented..
Publisher Example: #
Run the example/publisher.dart:
dart example/publisher.dart
copied to clipboard
import 'dart:async';
import 'package:dart_nats_streaming/dart_nats_streaming.dart';
void main() async {
final stan = Client();
stan.onConnect(function: () {
print('Stan connected..');
});
stan.onDisconnect(function: () {
print('Stan disconnected..');
});
await stan.connect(host: 'localhost', clusterID: 'my-cluster-id');
var counter = 0;
Timer.periodic(Duration(seconds: 1), (timer) {
counter++;
print('Publishing: this is an event (#$counter) on topic: test');
stan.pubString(subject: 'test', string: 'this is an event (#$counter}) on topic: test');
});
}
copied to clipboard
Subscriber Example: #
Run the example/publisher.dart:
dart example/publisher.dart
copied to clipboard
import 'package:dart_nats_streaming/dart_nats_streaming.dart';
void main() async {
final stan = Client();
stan.onConnect(function: () {
print('Stan connected..');
});
stan.onDisconnect(function: () {
print('Stan disconnected..');
});
await stan.connect(host: 'localhost', clusterID: 'my-cluster-id');
final subscription = await stan.subscribe(subject: 'test', queueGroup: 'my-queue-group', durableName: 'my-dourable-name');
await for (final dataMessage in subscription!.stream) {
print(dataMessage.asString());
stan.acknowledge(subscription, dataMessage);
}
}
copied to clipboard
Flutter Examples: #
Import and Declare object
import 'package:dart_nats_streaming/dart_nats_streaming.dart';
final stan = Client();
Subscription testSubscription;
copied to clipboard
Simply connect to server and subscribe to subject
void connect() async {
await stan.connect(host: 'localhost', clusterID: 'my-cluster-id');
testSubscription = await stan.subscribe(subject: 'test', queueGroup: 'my-queue-group', durableName: 'my-dourable-name');
}
copied to clipboard
Use as Stream in StreamBuilder
StreamBuilder(
stream: testSubscription.stream,
builder: (context, AsyncSnapshot<DataMessage> snapshot) {
stan.acknowledge(testSubscription, snapshot.data);
return Text(snapshot.hasData ? '${snapshot.data.asString()}' : '');
},
),
copied to clipboard
Publish Message
stan.pubString(subject: 'subject', string: 'message string');
copied to clipboard
This project is built on top of dart_nats and was heavily inspired and copied from the stan_dart.
Contributors and PR are welcome.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.