GithubHelp home page GithubHelp logo

aquinary / dart-nats Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chartchuo/dart-nats

0.0 0.0 0.0 195 KB

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

License: MIT License

Dart 100.00%

dart-nats's Introduction

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

Flutter Other Platform Support both TCP Socket and WebSocket

client.connect(Uri.parse('nats://localhost:4222'));
client.connect(Uri.parse('tls://localhost:4222'));
client.connect(Uri.parse('ws://localhost:80'));
client.connect(Uri.parse('wss://localhost:443'));

background retry

  // unawait 
   client.connect(Uri.parse('nats://localhost:4222'), retry: true, retryCount: -1);
  
  // await for connect if need
   await client.wait4Connected();

   // listen to status stream
   client.statusStream.lesten((status){
    // 
    print(status);
   });

Turn off 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('nats://localhost'));
  var sub = client.sub('subject1');
  await 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('nats://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

      await natsClient.pubString('subject','message string');

Request

var client = Client();
client.inboxPrefix = '_INBOX.test_test';
await client.connect(Uri.parse('nats://localhost:4222'));
var receive = await client.request(
    'service', Uint8List.fromList('request'.codeUnits));

Structure Request

var client = Client();
await client.connect(Uri.parse('nats://localhost:4222'));
client.registerJsonDecoder<Student>(json2Student);
var receive = await client.requestString<Student>('service', '');
var student = receive.data;


Student json2Student(String json) {
  return Student.fromJson(jsonDecode(json));
}

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'),
  connectOption: ConnectOption(
    nkey: 'UDXU4RCSJNZOIQHZNWXHXORDPRTGNJAHAHFRGZNEEJCPQTT2M7NLCNF4',
  ),
);

JWT Authentication

var client = Client();
client.seed =
    'SUAJGSBAKQHGYI7ZVKVR6WA7Z5U52URHKGGT6ZICUJXMG4LCTC2NTLQSF4';
client.connect(
  Uri.parse('nats://localhost'),
  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

dart-nats's People

Contributors

chartchuo avatar ngoluuduythai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.