actors library

actors is a library that enables the use of the Actors Model in Dart.

It is a thin wrapper around Dart's Isolate that makes them much easier to use.

An Actor can be created either from a simple function or a class that mixes in the Handler trait:


import 'package:actors/actors.dart';

final functionActor = Actor.of((String message) => "Hi $message");

class MyHandler with Handler<int, int> {
  Future<int> handle(int message) async => message * 2;

final basicActor = Actor(MyHandler());

Actor and Handler are the main types of this library. However, there are also a few other important types:

  • Messenger mixin is meant to provide a higher level abstraction than Actor, with the same API but without the constraint that the implementation must be fully isolated, or handle the message a single time only.
  • ActorGroup implements Messenger and can handle a message using multiple actors, one or more times, depending on its strategy.
  • LocalMessenger handles a message in the local Isolate (i.e. it's equivalent to a simple async function).


Actor<M, A>
An Actor is an entity that can send messages to a Handler running inside a Dart Isolate.
ActorGroup<M, A>
An ActorGroup groups a number of Actors using the same type of Handler to handle messages.
GroupStrategy<M, A>
A strategy for forwarding messages and selecting or computing an answer from a group of Messenger instances.
LocalMessenger<M, A>
A simple implementation of Messenger that runs in the local Isolate.
MultiHandler<M, A>
GroupStrategy that sends each message received to handlersPerMessage (m) Messengers, and requires 'minAnswers' (n) successful answers to provide a final, combined answer, where group size >= m >= n.
RoundRobin<M, A>
GroupStrategy that sends a message to a single member of the group, iterating over all members as messages are sent.
StreamActor<M, A>
An Actor that has the ability to return a Stream, rather than only a single object, for each message it receives.


Handler<M, A>
A Handler implements the logic to handle messages.
Messenger<M, A>
A Messenger can send a message and receive an answer asynchronously.
Sendable<M, A>
A handle to an Actor which can be sent to other actors.


asHandler<M, A>(HandlerFunction<M, A> handlerFunction) Handler<M, A> Function()
Wrap a HandlerFunction into a Handler object.


HandlerFunction<M, A> = FutureOr<A> Function(M message)

Exceptions / Errors

An Exception that is thrown by an Actor if it fails to initialize.
An Exception that indicates that the channel of communication with a Messenger has been broken.
An Exception that holds information about an Error thrown in a remote Isolate.