Global message bus (pub-sub) in Ember

This is more of an architectural question with an example.

I know Ember has many ways of communication but why is global pub/sub discouraged (not implemented)? Throwing actions seems like a one-way message bus to me.

Pub sub seems like a great use case for inter-component and service-components communication.

Simple example where pub/sub would be a simple solution:

  • I have a captcha service and a captcha component.
  • Captcha can be displayed on any route.
  • Once you land on a route, component will render, check if captcha is used and (if yes) fetch image from service and display it.
  • Once you solve the captcha you’ll click a button that belongs to a controller.
  • Controller verifies the captcha with service and then either destroys the component if captcha is good or captcha component should somehow re-fetch new image.

I currently don’t have a good idea how to solve this without pub sub pattern.

Is that what you need? http://www.thesoftwaresimpleton.com/blog/2015/04/27/event-bus/

I already found that, implemented it and it works but it’s not ‘the-ember-way’.

I primarily want to discuss best patterns for solving communication between components, service-components etc.

DDAU is nice but it’s really limiting when you want to instruct a component to ‘do-something’ and binding/toggling properties seems like a really bad way to solve that problem.

Since global message bus isn’t welcome in Ember is there a different equally good way to solve such issues?

I couldn’t agree more. We have the pubsub pattern a lot in our app (realtime, post messaging, etc). I know it’s becoming taboo, but DDAU can be a round peg in a square hole sometimes.

So our solution is to use Ember.Evented with a service. And sometimes for heavy message usage, we use a helper mixin. Here is a simplified Twiddle I made: Ember Twiddle

Sorry that got big quick. Pay attention to the services/bus.js and the two components message-pub-button and message-listener.

2 Likes

Introducing ember-message-bus, a simple message bus addon inspired by the @workmanw twiddle.

1 Like