Class: MessageBus

OO. MessageBus

Represents the Ooyala V3 Player Message Bus. Use message bus events to subscribe to or publish player events from video to ad playback.

When you create an OO.Player object (for example, myplayer = OO.Player.create(...) ), that object contains a Message Bus object named mb. For example, you would access the publish() method by calling myplayer.mb.publish(...).

new MessageBus()

Methods

addDependent(eventName, dependentEvent, subscriber, onMergeParams)

Enables you to send a publish or subscribe message that is dependent on a condition or event. For example, you might want to change the UI based on the location or time of day. This method blocks the event (eventName) until the dependent event (dependentEvent) fires. For more information and examples of usage, see Listening to a Message Bus Event.
Parameters:
Name Type Description
eventName String The name of the event.
dependentEvent String The name of the event that triggers the specified event name.
subscriber String The name of the subscriber to which the message bus will publish the event.
onMergeParams function (Optional) A function used to pass data to the handler for the dependent event. This function is only necessary if need to complete a computation before passing data to the dependent event handler. This function can take up to four arguments and returns an array of arguments to be passed into the dependent event listener.
Example
//  This blocks the PAUSED event from firing until
	    // the 'user_allowed_pause' event has fired 
		player.mb.addDependent(
		  OO.EVENTS.PAUSED, 
		  'user_allowed_pause', 
		  'example', 
		  function(){}
		);

intercept(eventName, subscriber, callback)

Enables you to subscribe to events to the message bus using a callback function that allows you to manipulate the event payload and name. The returned list of arguments from the callback can be used in subsequent event triggers. For more information and examples of usage, see Listening to a Message Bus Event.
Parameters:
Name Type Description
eventName String The name of the event to intercept.
subscriber String The name of the subscriber to which the message bus will publish the event.
callback function A function that returns a list of arguments used in subsequent event triggers. This allows you to manipulate the event payload and name. To cancel propagation of an event using an intercepter, return false instead of an array.
Example
In the following example we subscribe to the published message bus PLAY event, 
specify 'test-plugin' as the subscriber and specify a payload of 'hello'. 

We also include an intercept that swaps the string 'goodbye' into the payload
so that when the message bus publishes the PLAY event, the console outputs 'goodbye' instead of 'hello':

mb.subscribe(OO.EVENTS.PLAY, "test-plugin", function(eventName, payload) {  
   console.log(eventName+": "+payload); 
});

mb.publish(OO.EVENTS.PLAY, "hello");
     
// Console displays "play: hello"
        
mb.intercept(OO.EVENTS.PLAY, "test-plugin", function(eventName, payload) {
    return ["goodbye"]; 
});
             
//   Console displays "play: goodbye"

publish(eventName)

Enables you to publish events to the message bus.
Parameters:
Name Type Description
eventName String The name of the event. Comma-separated arguments for the event may follow the event name as needed.
Examples
myplayer.mb.publish(OO.EVENTS.PLAY);
myplayer.mb.publish(OO.EVENTS.WILL_CHANGE_FULLSCREEN,true);

subscribe(eventName, subscriber, callback)

Subscribe to an event published to the message bus.

Compatibility:

HTML5, Flash


Parameters:
Name Type Description
eventName String The name of the event.
subscriber String The name of the subscriber to which the message bus will publish the event.
callback function The function that will execute when the subscriber receives the event notification.
Examples
myplayer.mb.subscribe(OO.EVENTS.METADATA_FETCHED, 'example', function(eventName) {});
// Subscribes to all events published by the Message Bus
messageBus.subscribe("*", 'example', function(eventName) {});  

unsubscribe(eventName, subscriber, callback)

Unsubscribes from an event published to the message bus.

Compatibility:

HTML5, Flash


Parameters:
Name Type Description
eventName String The name of the event.
subscriber String The name of the subscriber to which the message bus will unsubscribe from the event.
callback function The function that normally executes when the subscriber receives the event notification.
Examples
messageBus.unsubscribe(OO.EVENTS.METADATA_FETCHED, 'example', function(eventName) {});
// Unsubscribes from all events published by the Message Bus
messageBus.unsubscribe("*", 'example', function(eventName) {});