DartDart SDK 1.4.3



View Supported Platforms

First, add the PubNub Dart SDK to your project's pubspec.yaml file:

dependencies:
  pubnub: ^1.0.0

Next, run pub get in your project.

Finally, import the pubnub.dart package:


import 'package:pubnub/pubnub.dart';
 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
import 'package:pubnub/pubnub.dart';

void main() {
  final pubnub = PubNub(
      defaultKeyset: Keyset(
          subscribeKey: 'SUBSCRIBE_KEY',
          publishKey: 'PUBLISH_KEY',
          uuid: UUID('MY_UUID')));

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

  myChannel.subscribe().messages.listen((envelope) {
    print(envelope.payload);
  });

  myChannel.publish({'hello': 'world'});
}
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:

Instantiate a new Pubnub instance. Only the subscribeKey is mandatory. Also include publishKey if you intend to publish from this instance, and the secretKey if you wish to perform PAM administrative operations from this Dart instance.

 

It is not a best practice to include the secretKey in client-side code for security reasons.

When you init with secretKey, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
var pubnub = PubNub(
    defaultKeyset:
        Keyset(subscribeKey: 'demo', publishKey: 'demo', uuid: UUID('demo')));

A Subscription contains a Dart Stream of 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');
Call pubnub.time() to verify the client connectivity to the origin:
var response = await pubnub.time();

// (handle errors... if no errors, continue to print the value...)

print('Received timetoken is $response.value');
pubnub.subscribe(channels: {'my_channel'});
The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.
Publish a message to a channel:
var myChannel = pubnub.channel('my_channel');
var response = await myChannel.publish({'hello': 'world'});
// handle errors
Get occupancy of who's here now on the channel by UUID:
Requires you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
// hereNow and whereNow will be implemented in a forthcoming release
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
pubnub.subscribe(
  channels: {'my_channel'},
  withPresence: true
);
The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
// OPTION 1
// Use history() if you want to fetch messages gradually.
// Messages 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


// OPTION 2
// Use messages() 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);
var subscription = pubnub.subscribe(channels: {'test'});
// Now, unsubscribe from the channel(s) to which we just subscribed:
subscription.unsubscribe();
// Or, to unsubscribe from all channels:
pubnub.unusbscribeAll();
The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.