A Dart client for the NATS messaging system

  Dart

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.tcpConnect('localhost');
client.connect(Uri.parse('ws://localhost:80'));

API Change from version 0.2.x

To support Flutter Web. We change default transport from TCP socket to WebSocket. TCP Socket still able to access by client.tcpConnect(); and we commit to maintain both transport Migration from 0.2.x just change client.connect() to client.tcpConnect()

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('wss://demo.nats.io:443');
    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

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
  •  – 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
  •  – NATS 2.2

Download Dart-NATS client package source code on GitHub