David Pelaez edited this page Jul 13, 2015 · 14 revisions

Notifications let actors subscribe and publish to a topic without having to know the registry name or have a reference to other actors. Notifications are delivered to subscribed actors asynchronously.

The code is a simplified and tweaked version of ActiveSupport::Notifications.

Actors need to include Celluloid::Notifications to participate. They can subscribe(topic_string, method) and publish(topic_string, *payload). Notifications are sent to subscribers as async calls.

But publish calls can be made from outside actors also, using the Celluloid::Notifications.publish class method, or Celluloid.publish for short.

subscribe returns a subscriber object that can be used to unsubscribe later. However the caveat to ActiveSupport::Notifications still applies here: the implementation is designed for long-lived subscriptions and might not be efficient for many short-lived subscriptions.

The default notifier sends notifications to all subscribers, but can be swapped out for another implementation by setting Celluloid::Notifications.notifier. Notifiers should probably be actors since other implementations may rely on that (dcell for example may need an actor notifier for distributed pubsub).

The default notifier also links to subscribers so their subscriptions can be pruned when they die.

Add require 'celluloid/autostart' to start default notifier.

Check out the pubsub.rb example:

Examples in Reel:

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.