HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Publish and subscribe messaging system

Describes how the Dojo publish and messaging system is used for components in the Optimizely Content Management System (CMS) user interface.

Optimizely Content Management System (CMS) uses the Dojo publish and subscribe messaging system to pass events or notifications to components in the user interface. The Dojo toolkit provides functionality to facilitate this kind of communication because Dojo promotes class declaration and modular development, and one of the main mechanisms for ensuring that modules remain decoupled is to use this publish and subscribe messaging system.

Implement client-side messaging

Publish or subscribe is a well-known pattern for promoting decoupled messaging and communication between independent application building blocks. The decoupling achieved with publish and subscribe results from using the central messaging hub in Dojo and publishing and subscribing on specific topics rather than on specific objects.

For example, suppose you want to create a widget to display statistics about the current page from Google Analytics. The widget needs to know when the page changes to update what it displays to reflect the current page. You do not know what widgets are on the page or which can cause the context to change, so you cannot monitor events on any particular objects. However, there is a message with the /epi/cms/contextchanged topic that gets published on any context change, so the widget can subscribe to that topic to update its context.

postCreate: function () {
    this._contextChangedHandler = topic.subscribe('/epi/cms/contextchanged', this, this._onContextChange);
  },

  _onContextChange: function (context, caller) {
    // Widget will update itself using the new context.
  }

Whenever another component publishes a message with the same topic, it invokes the following code.

topic.publish('/epi/cms/contextchanged', [context]);

You can add modules to the system and monitor existing events without changing existing code. Many objects can monitor the events of one particular object without needing that object to keep track of who is monitoring it, as you might in a normal observable pattern.

Public topics

ComponentDescription
/epi/cms/requestcontextchangeRequests the application to try to change the context to a new one by passing it some information about the new context, pageLink, for example, and a reference to the object requesting the change.
/epi/cms/contextchangedThis occurs when the current context is changed. It returns the new context along with a reference to the object that originally requested the change.
/epi/cms/contextchangefailedThis function occurs when a context change request fails. It returns the current context along with a reference to the object that originally requested the change and the parameters passed with the context change request.