pubnub 0.1.1

  • Installing
  • Versions
  • 12

PubNub for Dart #

This is a Dart wrapper for only the basic PubNub functionality.

Currently you can do the following:

  • Connect to PubNub
  • Begin listening to a channel
  • React to messages in a channel
  • Post a message to a channel

This is enough to cover their "Getting Started Guide".

Getting Started (Dart) #

Here is PubNub's "Getting Started Guide", translated to Dart.

First, you need to include PubNub's JavaScript file in your host HTML site:

<!-- Include the PubNub Library -->
<script src="[version number].js"></script>

Be sure to replace [version number] with the current version. At the time of this writing it's 4.1.0. So, it would read like this:

<script src=""></script>

Include this Dart library:

import 'package:pubnub/pubnub.dart';

Instantiate PubNub:

var pubnubDemo = new PubNub(
    publishKey: '[your-publish-key]',
    subscribeKey: '[your-subscribe-key]');

(Of course replace [your-XXX-key] with your personal keys from PubNub).

In order to send and receive JSON structures you need the JS interop package. With it, you can declare an anonymous JS class / JSON structure like this:

library example;

import 'package:js/js.dart';

class ColorMsg {
  external String get color;
  external factory ColorMsg({String color});

This is the equivalent of the JSON structure in the original guide:

  color: 'someColor'

Add a listener:

pubnubDemo.addListener((PubNubEvent<ColorMsg> event) {
  ColorMsg msg = event.message;

The listener above assumes that the messages you receive have only one type. You can leave out the generic type if you need more flexibility:

pubnubDemo.addListener((PubNubEvent event) {
  var msg = event.message;

(Note that the `PubNubEvent instance is not the message that you post to the channel. This instance wraps the message, and contains additional PubNub information).

Subscribe to a channel:


Publish to a channel:

pubnubDemo.publish(channel: 'demo_tutorial',
        message: new ColorMsg(color: 'blue'));

(Note again that we are sending the "JS interop" derived class from above. You can not send Dart Map or other Dart objects directly).

And that's it. Have fun with your real-time Dart apps!

Changelog #

0.1.0 #

  • Initial version, covers basic functionality.
  • Added documentation.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  pubnub: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:pubnub/pubnub.dart';
Version Uploaded Documentation Archive
0.1.1 Oct 15, 2016 Go to the documentation of pubnub 0.1.1 Download pubnub 0.1.1 archive
0.1.0 Oct 14, 2016 Go to the documentation of pubnub 0.1.0 Download pubnub 0.1.0 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0