pubnub 0.1.1

  • Installing
  • Versions
  • 36

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!



  • 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


We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3


Describes how popular the package is relative to other packages. [more]
0 / 100
Code health derived from static analysis. [more]
99 / 100
Reflects how tidy and up-to-date the package is. [more]
32 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: web, other

Primary library: package:pubnub/pubnub.dart with components: js.


  • Fix .analysis_options.

    We were unable to parse .analysis_options.

  • Package is getting outdated.

    The package was released 87 weeks ago.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use pubnub.dart.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 hint.

    Run dartfmt to format lib/pubnub.dart.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

  • Enable strong mode analysis.

    Strong mode helps you to detect bugs and potential issues earlier.Start your analysis_options.yaml file with the following:

      strong-mode: true


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0
browser >=0.10.0 <0.11.0 0.10.0+3
js ^0.6.0 0.6.1