The Cross-Player Prebid Component communicates with the publisher’s player using window.postMessage as supported by all browsers. The message can be initiated either by the Component or by the Player.
The following rules apply for this communication:
window.postMessage ('hello', '*');top.postMessage ('hello', '*');window.addEventLIstener('message', listenerHandler);The communication between the Component and the Player uses two messages, one from the Player to request the Prebid results and the other from the Component to respond to the request.
This message is sent by the Player to request the prebid results from Prebid Component. The message should be constructed as a JSON object with the following fields:
PPCP:prebidUrlRequest.The message should be converted to a String using JSON.stringify before passing it into the postMessage.
This message is sent by the Component to the Player in response to the Player’s request for Prebid results. The message should be constructed as a JSON object with the following fields:
PPCP:prebidResponse.The message should be converted to a String using JSON.stringify before passing it into the postMessage.
NOTE: These steps assume that the Component is being loaded in the header and the Player is loaded in an IFrame.
PPCP:prebidUrlRequest.
Filter the messages looking for this command inside the requestHandler.window.addEventListener('message', requestHandler);
Request Handler Sample Code
// the component code is written in ES6 Javascript
this.requestHandler = (event) => {
if (event && event.data) {
let data;
try {
data = JSON.parse(event.data);
}
catch (err) {
// Invalid message data. Ignore it.
return;
}
const frameWnd = event.source;
// internal function used to send back the response
const sendResponse = (url) => {
// send response to source window (iframe)
Logger.log(this._prefix, 'Post url:', url);
const response = {
command: 'PPCP:prebidResponse',
messageId: data.messageId,
url: url
};
const sendingData = JSON.stringify(response);
frameWnd.postMessage(sendingData, '*');
};
// sync request
if (data.command === 'PPCP:prebidUrlRequest') {
if (this.defaultUrl) {
// we have ad URL to play
sendResponse(this.defaultUrl);
}
else if (this.defaultUrl === null) {
// failed to get ad URL
sendResponse('failed');
}
else {
// prebid.js is not ready (not loaded yet)
sendResponse('unknown');
}
}
}
};
PPCP:prebidResponse.
Filter the messages looking for this command inside the responseHandler.window.addEventListener('message', responseHandler);
PPCP:prebidUrlRequest to request Prebid results from the Component.PPCP:prebidUrlRequestNOTE: The * argument indicates post the message to any domain.
const msg = {};
msg.command = 'PPCP:prebidUrlRequest';
msg.messageId = 1234;
top.postMessage(JSON.stringify(msg), '*');
What: The Component will post a response message back to the Player’s IFrame, sending the current status of the Prebid process.
If the Component posts the message, indicating that it does not yet know the results, the Player should be prepared to request the results again.
PPCP:prebidUrlRequest message, it will post a response message back to the Player’s IFrame, sending the current status of the Prebid process. The URL field of the response will indicate what the Component knows at that time about the Prebid process. Possible values:
PPCP:prebidResponseNOTE: The * argument indicates post the message to any domain.
var msg = {};
msg.command = 'PPCP:prebidResponse';
msg.messageId = 1234;
msg.url = 'http://url-to-vast.xml'
top.postMessage(JSON.stringify(msg), '*');
PPCP:prebidUrlRequest to request Prebid results from the Component.
event.data in the listener handler using JSON.parse. If event.data includes the event.data.command 'PPCP:prebidResponse', it should examine event.data.url and event.data.messageId.messageId matches the messageId that it used in the request for Prebid response.event.data.url field contains a valid URL, the Player should use that URL to retrieve the ad to play.event.data.url contains the value failed, then Prebid.js was unable to find an ad and, as a result, the Player should continue with its normal content.event.data.url contains the value unknown, then the Player should send out another request for the Prebid results after waiting a brief period of time.var lastMessageId = 12345;
var responseHandler = function(event) {
if (event && event.data) {
var data = JSON.parse(event.data);
if (data.command === ‘PPCP:prebidResponse’ && data.messageId === lastMessageId) {
if (data.url === ‘unknown’) {
// the prebid process is still pending
setTimeout(function () {
// request url
// ...
}, 100);
}
else if (data.url === ‘failed’) {
// the prebid process did not return an ad
// continue main content
// ...
}
else if (typeof data.url === ‘string’ && data.url.length > 0) {
// prebid successfully selected an ad to play
// play ad
// ...
}
}
}
};