dart_nats 0.4.9 dart_nats: ^0.4.9 copied to clipboard
A Dart client for the NATS messaging system. Design to use with Dart and flutter.
TLS, Token, User/Pass, NKey, JWT support NOW. #
Dart-NATS #
A Dart client for the NATS messaging system. Design to use with Dart and flutter.
Flutter Web Support by WebSocket #
client.connect(Uri.parse('ws://localhost:80'));
Flutter Other Platform Support by TCP Socket and WebSocket #
client.connect(Uri.parse('nats://localhost'));
client.connect(Uri.parse('ws://localhost:80'));
Turn off background retry and catch exception #
try {
await client.connect(Uri.parse('nats://localhost:1234'), retry: false);
} on NatsException {
//Error handle
}
Dart Examples: #
Run the example/main.dart
:
dart example/main.dart
import 'package:dart_nats/dart_nats.dart';
void main() async {
var client = Client();
client.connect(Uri.parse('ws://localhost:80'));
var sub = client.sub('subject1');
client.pubString('subject1', 'message1');
var msg = await sub.stream.first;
print(msg.string);
client.unSub(sub);
client.close();
}
Flutter Examples: #
Import and Declare object
import 'package:dart_nats/dart_nats.dart' as nats;
nats.Client natsClient;
nats.Subscription fooSub, barSub;
Simply connect to server and subscribe to subject
void connect() {
natsClient = nats.Client();
natsClient.connect(Uri.parse('ws://hostname');
fooSub = natsClient.sub('foo');
barSub = natsClient.sub('bar');
}
Use as Stream in StreamBuilder
StreamBuilder(
stream: fooSub.stream,
builder: (context, AsyncSnapshot<nats.Message> snapshot) {
return Text(snapshot.hasData ? '${snapshot.data.string}' : '');
},
),
Publish Message
natsClient.pubString('subject','message string');
Dispose
void dispose() {
natsClient.close();
super.dispose();
}
Authentication #
Token Authtication
var client = Client();
client.connect(Uri.parse('nats://localhost'),
connectOption: ConnectOption(authToken: 'mytoken'));
User/Passwore Authentication
var client = Client();
client.connect(Uri.parse('nats://localhost'),
connectOption: ConnectOption(user: 'foo', pass: 'bar'));
NKEY Authentication
var client = Client();
client.seed =
'SUACSSL3UAHUDXKFSNVUZRF5UHPMWZ6BFDTJ7M6USDXIEDNPPQYYYCU3VY';
client.connect(
Uri.parse('nats://localhost'),
retryInterval: 1,
connectOption: ConnectOption(
nkey: 'UDXU4RCSJNZOIQHZNWXHXORDPRTGNJAHAHFRGZNEEJCPQTT2M7NLCNF4',
),
);
JWT Authentication
var client = Client();
client.seed =
'SUAJGSBAKQHGYI7ZVKVR6WA7Z5U52URHKGGT6ZICUJXMG4LCTC2NTLQSF4';
client.connect(
Uri.parse('nats://localhost'),
retryInterval: 1,
connectOption: ConnectOption(
jwt:
'''eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJBU1pFQVNGMzdKS0dPTFZLTFdKT1hOM0xZUkpHNURJUFczUEpVT0s0WUlDNFFENlAyVFlRIiwiaWF0IjoxNjY0NTI0OTU5LCJpc3MiOiJBQUdTSkVXUlFTWFRDRkUzRVE3RzVPQldSVUhaVVlDSFdSM0dRVERGRldaSlM1Q1JLTUhOTjY3SyIsIm5hbWUiOiJzaWdudXAiLCJzdWIiOiJVQzZCUVY1Tlo1V0pQRUVZTTU0UkZBNU1VMk5NM0tON09WR01DU1VaV1dORUdZQVBNWEM0V0xZUCIsIm5hdHMiOnsicHViIjp7fSwic3ViIjp7fSwic3VicyI6LTEsImRhdGEiOi0xLCJwYXlsb2FkIjotMSwidHlwZSI6InVzZXIiLCJ2ZXJzaW9uIjoyfX0.8Q0HiN0h2tBvgpF2cAaz2E3WLPReKEnSmUWT43NSlXFNRpsCWpmkikxGgFn86JskEN4yast1uSj306JdOhyJBA''',
),
);
Full Flutter sample code example/flutter/main.dart
Features #
The following is a list of features currently supported:
- ✅
- Publish
- ✅
- Subscribe, unsubscribe
- ✅
- NUID, Inbox
- ✅
- Reconnect to single server when connection lost and resume subscription
- ✅
- Unsubscribe after N message
- ✅
- Request, Respond
- ✅
- Queue subscribe
- ✅
- Request timeout
- ✅
- Events/status
- ✅
- Buffering message during reconnect atempts
- ✅
- All authentication models, including NATS 2.0 JWT and nkey
- ✅
- NATS 2.x
- ✅
- TLS
Planned:
- ❌
- Connect to list of servers