wallforfry / dart_mercure

Dart library for dunglas/mercure
BSD 2-Clause "Simplified" License
13 stars 4 forks source link
dart flutter mercure push real-time server-sent-events

Dart library for dunglas/mercure

pipeline status Dart CI pub package

license.

Usage

A basic usage example of publish/subscribe :

import 'package:dart_mercure/dart_mercure.dart';

main() {

  // Token generate with "mercure" JWT_KEY
  var token = 'YOUR_JWT_TOKEN';
  var hub_url = 'http://MERCURE_HUB_URL/.well-known/mercure';
  var topic = 'http://YOUR_TOPIC/FOO';

  var mercure = Mercure(token: token, hub_url: hub_url);

  // Subscribes to topics
  mercure.subscribeTopics(topics: <String> [topic, 'ANOTHER_TOPIC'], onData: (Event event) {
    print(event.data);
  });

  // Subscribe to topic
  mercure.subscribeTopic(topic: topic, onData: (Event event) {
    print(event.data);
  });

  // Publish on topic
  mercure.publish(topic: topic, data: 'DATA').then((status) {
    if(status == 200) {
      print('Message Sent');
    }
    else {
      print('Message Failed with code : $status');
    }
  });
}

Features and bugs

Please file feature requests and bugs at the issue tracker