dart_message_bus 0.0.1

  • README.md
  • Installing
  • Versions
  • new48


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.


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.



  • Initial version

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

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or packages 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';
Version Uploaded Documentation Archive
0.0.1 Mar 14, 2018 Go to the documentation of dart_message_bus 0.0.1 Download dart_message_bus 0.0.1 archive


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

  • Dart: 2.0.0-dev.31.0
  • pana: 0.10.3


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


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:dart_message_bus/dart_message_bus.dart.


  • 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 dart_message_bus.dart.

  • Fix issues reported by dartanalyzer or dartfmt.

    dartanalyzer or dartfmt reported 3 hints.

    Run dartfmt to format lib/dart_message_bus.dart.

    Run dartfmt to format lib/src/future_message.dart.

    Similar analysis of the following files failed:

    • lib/src/message_bus.dart (hint)


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0
Dev dependencies
test ^0.12.0