Giter Site home page Giter Site logo

dart's Introduction

PubNub Dart SDK

This is the official PubNub Dart SDK repository.

PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.

This repository contains the following packages:

  • pubnub Pub Version - 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.

  • pubnub_flutter - a collection of widgets for PubNub Dart SDK that allows you to create PubNub powered cross-platform applications with ease.

Get keys

You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.

Configure PubNub

  1. Integrate the Dart SDK into your project using the pub package manager by adding the following dependency in your pubspec.yml file:

    dependencies:
      pubnub: ^3.2.0

    Make sure to provide the latest version of the pubnub package in the dependency declaration.

  2. From the directory where your pubspec.yml file is located, run the dart pub get or flutter pub get command to install the PubNub package.

  3. Configure your keys:

    var pubnub = PubNub(
      defaultKeyset:
          Keyset(subscribeKey: 'mySubscribeKey', publishKey: 'myPublishKey', uuid: UUID('ReplaceWithYourClientIdentifier')));

Add event listeners

/*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) {
    switch (envelope.messageType) {
      case MessageType.normal:
          print('${envelope.publishedBy} sent a message: ${envelope.content}');
          break;
      case MessageType.signal:
          print('${envelope.publishedBy} sent a signal message: ${envelope.content}');
        break;
      case MessageType.objects:
          print('object event received from ${envelope.publishedBy} with data ${envelope.payload['data']}');
        break;
      case MessageType.messageAction:
          print('message action event ${envelope.payload['event']} received with data ${envelope.payload['data']}');
        break;
      case MessageType.file:
          var fileInfo = envelope.payload['file'];
          var id = fileInfo['id']; // unique file id
          var name = fileInfo['name']; // file name
          print('${envelope.publishedBy} sends file $name with message  ${envelope.payload['message']}');
        break;
      default:
        print('${envelope.publishedBy} sent a message: ${envelope.content}');
    }
  });

  subscription.presence.listen((event) {
      print('''Presence Event with action: ${event.action},
      received from uuid: ${event.uuid}
      with time token: ${event.timetoken},
      Total Occupancy now is: ${event.occupancy}
      ''');
  });

var envelope =
    await subscription.messages.firstWhere((envelope) => envelope.channel == 'my_channel');

Publish/subscribe

var channel = "getting_started";
var subscription = pubnub.subscribe(channels: {channel});

await pubnub.publish(channel, "Hello world");

Documentation

Support

If you need help or have a general question, contact [email protected].

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.