dart_message_bus 0.0.1

  • Readme
  • Changelog
  • Installing
  • 20

dart_message_bus #

A useful implementation of publish–subscribe messaging pattern using Dart Stream.

(From wikipedia:) In software architecture, publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead categorize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are. Read more on wiki.

In this particular implementation, I changed the messages types to a strongly-typed class named Message which holds some useful information about message and the key for listening and publishing messages is a String key which made it easier to identify and group messages.

Usage #

simple usage example: #

import 'package:dart_message_bus/dart_message_bus.dart';

main() {
  // usually the MessageBus is a singleton shared instance
  final bus = new MessageBus();

  // in service A which is a subscriber
  bus.subscribe('message-key', (Message m) {
    // use m:
    var data = m.data;
    // etc.

  // in service B which is a publisher
  var data = new YourOriginalMessageDataClass();
  var message = new Message('message-key', data: data);

Advanced scenario #

Say ServiceA publishes a message, and wants to wait for say 30 seconds for a certain message-back, and then continue executing -no matter there was a message-back or not. The ServiceB is one of the subscribers, and when he received the message, makes a http-request asynchronously -say it may takes 1 to 60 seconds- and when the request got completed, it publishes a specified message -which actually is a message-back for ServiceA. The snippet below will do the work:

import 'package:dart_message_bus/dart_message_bus.dart';

class ServiceA {
  final MessageBus _bus;
  Future<Null> run() async {
    var callbackMessage = await _bus.publish(
      new Message('message-a', data: new DataA()),
      waitForKey: 'message-b',
      timeout: const Duration(seconds: 30)
    if(callbackMessage.isEmpty) {
      // means the another service's message didnot received
      // and timeout occured.
    } else {
      // the callback from another service received
      // and callbackMessage.data contains callback-data.

class ServiceB {
  final MessageBus _bus;
  Future<Null> run() async {
    _bus.subscribe('message-a', (Message m) async {
      // when this service received a 'message-a' message,
      // it performs a long-time action. For example
      // calling an API. To simulate latency we are
      // delaying with this line:
      await new Future.delayed(const Duration(seconds: 5));

      // after the long-time action completed, this
      // service publishes a new message names 'message-b':
      var data = new DataB();
      var message = new Message('message-b', data: data);

main() {
  // usually the MessageBus is a singleton shared instance
  final bus = new MessageBus();

  var serviceA = new ServiceA(bus);
  var serviceB = new ServiceB(bus);

  // first, run serviceB to subcribe to message-a

  // then you can run serviceA to publish message-a
  // and also receive message-b if there is any

For a complete scenario please clone the git repo and see and run tests.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

0.0.1 #

  • Initial version

Use this package as a library

1. Depend on it

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

  dart_message_bus: ^0.0.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:dart_message_bus/dart_message_bus.dart';
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 https://dart.dev/dart-2#migration.

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.20.1 <2.0.0