HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

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) makes use of the Dojo publish and subscribe messaging system for passing 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/subscribe is a well-known pattern for promoting decoupled messaging and communication between independent application building blocks. The decoupling achieved with publish and subscribe is a result of 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 currently 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.
     }

This code is invoked any time another component publishes a message with the same topic.

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

You can add new modules to the system and monitor existing events without changing any existing code, and many objects can monitor the events of one particular object without the need for 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/contextchangedOccurs when the current context is changed. Returns the new context along with a reference to the object which originally requested the change.
/epi/cms/contextchangefailedOccurs when a context change request has failed. Returns the current context along with a reference to the object which originally requested the change and the parameters passed with the context change request.