PubNub Dart SDK

Pub Version

pubnub is a Flutter-friendly SDK written in Dart that allows you to connect to PubNub Data Streaming Network and add real-time features to your application.

Installation

Using pub dependency management tool

pubnub uses the standard pub tool for package management to distribute Dart code.

To add the package to your Dart or Flutter project, add pubnub as a dependency in your pubspec.yaml.

dependencies:
  pubnub: ^6.0.2

After adding the dependency to pubspec.yaml, run the dart pub get command in the root directory of your project (the same that the pubspec.yaml is in).

Using Git

If you want to use the latest, unreleased version of pubnub, you can add it to pubspec.yaml as a Git package.

dependencies:
  pubnub:
    git: git://github.com/pubnub/dart.git
    path: pubnub

Using a local copy of the repository

If you want to copy the Dart repository and modify it locally, clone it using git clone https://github.com/pubnub/dart and then import it into pubspec.yaml as follows:

dependencies:
  pubnub:
    path: ../path-to-cloned-pubnub-repo

Importing

After you install the pubnub package, import it in your application. You can import it in one of two ways:

// Import all PubNub objects into your namespace
import 'package:pubnub/core.dart';

// Or import PubNub into a named namespace
import 'package:pubnub/pubnub.dart' as pn;

Usage

Keyset

First, create a Keyset instance:

final myKeyset = Keyset(
  subscribeKey: 'demo',
  publishKey: 'demo',
  uuid: UUID('demo'));

Keyset contains all your configuration. You can use multiple Keysets (with different parameters) if you need.

If you have a PubNub account, replace demo with the key values from your PubNub dashboard. If you don't, you can use the highly rate-limited demo keys, but be aware that the keyset is public, so don't send any sensitive data.

PubNub instance

Next, instantiate the PubNub class, passing myKeyset as a default keyset. This will be used any time a keyset is not passed into a method.

final pubnub = PubNub(defaultKeyset: myKeyset);

Now you can use the pubnub instance to publish messages, subscribe to channels, and everything else!

Publishing messages

To publish a message, use the publish method.

pubnub.publish('my_channel', { 'content': 'Hello world!' });

Messages can be any JSON-serializable object.

If you are going to publish a lot of messages to one channel, you can use channel abstraction to obtain an instance of a Channel.

final myChannel = pubnub.channel('my_channel');

myChannel.publish(200);
myChannel.publish({ 'answer': 42 });

Subscribing to channels

To subscribe to a list of channels or channel groups, use the subscribe method. You need to pass a Set of channels or channel groups.

var subscription = pubnub.subscribe(channels: {'ch1', 'ch2'});

You can also use your Channel instance:

var subscription = myChannel.subscribe();

Both of those methods return a Subscription.

A Subscription contains a Dart Stream messages from the channel(s) to which you are subscribed. You can transform that stream in the usual ways, or add a listener using listen:

subscription.messages.listen((envelope) {
  print(`${envelope.uuid} sent a message: ${envelope.payload}`);
});

var envelope =
      await sub.messages.firstWhere((envelope) => envelope.channel == 'ch2');

Channel history

You can retrieve past messages from a channel in two ways, as follows:

Using channel.history

Use this method if you want to fetch messages gradually. They are fetched in descending order (from newest to oldest) by default.

var history = myChannel.history(chunkSize: 50);

await history.more();
print(history.messages.length); // 50
await history.more();
print(history.messages.length); // 100

Using channel.messages

Use this method to fetch many messages at once.

var history = myChannel.messages(from: Timetoken(1234567890));

var count = await history.count();
print(count);

var messages = await history.fetch();
print(messages.length);

await history.delete(); // Beware! This will delete all messages matched

Multiple keysets

There are two ways to use multiple keysets at the same time, as follows:

Using named keysets

You can add multiple keysets with a name to an instance of PubNub.

pubnub.keysets.add(myKeyset1, name: 'keyset1');
pubnub.keysets.add(myKeyset2, name: 'keyset2');

To use a named keyset instead of the default, pass its name in a using: parameter into one of the pubnub instance methods:

pubnub.publish('channel', 42, using: 'keyset1');
var myChannel = pubnub.channel('channel', using: 'keyset2');

Using a keyset instance

Instead of adding the keyset to pubnub.keysets, you can use the keyset: named parameter to pass a keyset instance directly to pubnub instance methods:

pubnub.subscribe(channels: {'channel'}, keyset: myKeyset1)

Contributing

  1. Clone the repository.

    git clone https://github.com/pubnub/dart.git
    
  2. Enter the directory and install dependencies.

    cd dart
    dart pub get
    
  3. Run the build_runner to generate necessary source files.

    dart pub run build_runner build
    

Libraries

pubnub
PubNub is an SDK that allows you to communicate with PubNub Data Streaming Network in a fast and easy manner.
src/core/crypto/cipher_key
src/core/crypto/crypto
src/core/endpoint
src/core/exceptions
src/core/keyset/keyset
src/core/keyset/store
src/core/logging/logging
src/core/message/base_message
src/core/message/message
src/core/message/message_type
src/core/net/exceptions
src/core/net/net
src/core/net/request
src/core/net/request_handler
src/core/net/request_type
src/core/net/response
src/core/parser
src/core/policies/retry_policy
src/core/supervisor/event
src/core/supervisor/fiber
src/core/supervisor/resolution
src/core/supervisor/signals
src/core/supervisor/supervisor
src/core/timetoken
src/core/user_id
src/core/uuid
src/crypto/aesCbcCryptor
src/crypto/crypto
src/crypto/cryptoConfiguration
src/crypto/cryptorHeader
src/crypto/encryption_mode
src/crypto/legacyCryptor
src/default
src/dx/_endpoints/channel_group
src/dx/_endpoints/files
src/dx/_endpoints/message_action
src/dx/_endpoints/objects/channel_metadata
src/dx/_endpoints/objects/membership_metadata
src/dx/_endpoints/objects/uuid_metadata
src/dx/_endpoints/presence
src/dx/_endpoints/publish
src/dx/_endpoints/push
src/dx/_endpoints/signal
src/dx/_endpoints/time
src/dx/_utils/default_flow
src/dx/_utils/default_result
src/dx/_utils/ensure
src/dx/_utils/exceptions
src/dx/_utils/file_validation
src/dx/_utils/signature
src/dx/_utils/time
src/dx/_utils/utils
src/dx/batch/batch
src/dx/channel/channel
src/dx/channel/channel_group
src/dx/channel/channel_history
src/dx/files/extensions/keyset
src/dx/files/files
src/dx/files/schema
src/dx/logging_configuration
src/dx/message_action/message_action
src/dx/objects/channel_metadata
src/dx/objects/objects
src/dx/objects/objects_types
src/dx/objects/schema
src/dx/objects/uuid_metadata
src/dx/pam/cbor
src/dx/pam/extensions/keyset
src/dx/pam/pam
src/dx/pam/resource
src/dx/pam/token
src/dx/pam/token_request
src/dx/presence/presence
src/dx/publish/publish
src/dx/push/push
src/dx/signal/signal
src/dx/supervisor/supervisor
src/dx/time
src/logging/logging
src/networking/meta/diagnostics/diagnostics
src/networking/meta/diagnostics/stub
src/networking/meta/meta
src/networking/meta/strategy
src/networking/networking
src/networking/request_handler/request_handler
src/networking/request_handler/stub
src/networking/response/response
src/networking/response/stub
src/parser/parser
src/subscribe/_endpoints/subscribe
src/subscribe/envelope
src/subscribe/extensions/keyset
src/subscribe/manager
src/subscribe/subscribe
src/subscribe/subscribe_loop/subscribe_fiber
src/subscribe/subscribe_loop/subscribe_loop
src/subscribe/subscribe_loop/subscribe_loop_state
src/subscribe/subscription

Modules

crypto Modules
Default cryptography module used by PubNub SDK.
logging Modules
Default logging module used by PubNub SDK.
networking Modules
Default networking module used by PubNub SDK.