dart_nats_client 0.1.11 copy "dart_nats_client: ^0.1.11" to clipboard
dart_nats_client: ^0.1.11 copied to clipboard

outdated

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 #

A Dart client for the NATS messaging system. Design to use with Dart and flutter.

Dart Examples: #

Run the example/main.dart:

dart example/main.dart

Simple poll example #

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 #

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.getStream().listen((msg) {
      print(msg.string);
    });
  // Publish string to topic
  client.pubString('subject1', 'message1');
  // Some delay for receiving
  await Future.delayed(Duration(milliseconds: 100));
  // Unsubscribe from topic
  sub.unSub();
  // Close client connection
  client.close();
}

Flutter Examples #

Import and Declare object

import 'package:dart_nats_client/dart_nats_client.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('demo.nats.io');
    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();
  }

Full Flutter sample code example/flutter/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 pub run test 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
3
likes
0
pub points
0%
popularity

Publisher

unverified uploader

A Dart client for the NATS messaging system. Design to use with Dart and flutter. Forked from https://github.com/chartchuo/dart-nats

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on dart_nats_client