natsio 0.1.1
natsio: ^0.1.1 copied to clipboard
A pure Dart client for NATS, ported from nats.go
natsio (Dart) #
A pure Dart client for NATS, ported from nats.go. Supports core NATS, headers, request/reply, JetStream, Key-Value, Object Store, and Micro.
Install #
Add to pubspec.yaml:
dependencies:
natsio: ^0.1.0
If you are consuming from Git:
dependencies:
natsio:
git:
url: git@github.com:policyaid/nats.dart.git
Quick Start #
import 'package:natsio/nats.dart';
Future<void> main() async {
final client = Client(
options: ClientOptions(servers: ['nats://127.0.0.1:4222']),
);
await client.connect();
final sub = client.subscribe('greet');
sub.listen((msg) {
print('got: ${msg.stringData}');
});
client.publishString('greet', 'hello');
await client.flush();
await client.drain();
}
Request/Reply #
final msg = await client.requestString(
'echo',
'ping',
timeout: Duration(seconds: 1),
);
print(msg.stringData);
Headers and No-Responders #
client.publish(
'foo',
Uint8List.fromList([1, 2, 3]),
headers: {'X-Trace': ['abc123']},
);
final msg = await client.requestString('svc.ping', 'hi');
if (msg.isNoResponders) {
print('no responders');
}
JetStream #
final js = JetStream(client);
await js.addStream(StreamConfig(
name: 'EVENTS',
subjects: ['events.>'],
));
await js.publishString('events.1', 'hello');
Key-Value #
final js = JetStream(client);
final kv = await js.keyValue('CONFIG');
await kv.putString('feature', 'on');
final entry = await kv.get('feature');
print(entry?.stringValue);
Object Store #
final js = JetStream(client);
final store = await js.objectStore('ASSETS');
await store.putString('logo.txt', 'hello');
final obj = await store.getString('logo.txt');
print(obj);
Running NATS Locally #
docker run --rm -p 4222:4222 -p 8222:8222 -p 6222:6222 nats:latest -js
Testing #
cd nats.dart
dart test
Integration tests will start a Docker NATS server automatically if nats-server is not in PATH.