Skip to main content
Version: 7.2.0

Sending messages from/to Sender to/from Receiver

This article will explain how to send data to an active receiver and how to respond on the receiver.

On our Web SDK, this functionality was implemented from 2.79.0 onwards.

Send data to the receiver

The Google Cast SDK has a global object that everyone can access. This global object is also being used by THEOplayer. The general idea is to retrieve the current active cast session and send a message on a specific namespace.

Web SDK
// Retrieve the current cast session
var castSession = cast.framework.CastContext.getInstance().getCurrentSession();
// Send message on defined namespace channel
castSession.sendMessage("namespace", "message");

Reference:

Android SDK
// Retrieve the current cast session
CastSession castSession = CastContext.getSharedInstance(this).getSessionManager().getCurrentCastSession();
// Send message on defined namespace channel
castSession.sendMessage("namespace", "message");

Reference:

iOS/tvOS SDK and Legacy iOS/tvOS SDK (4.12.x)

On iOS, we need a component that implements the GCKGenericChannelDelegate protocol.

// Retrieve the current cast session
let castSession = GCKCastContext.sharedInstance().sessionManager.currenCastSession
// Create the defined namespace channel
let channel = GCKGenericChannel(namespace: "namespace")
// Set the delegate for the namespace channel (GCKGenericChannelDelegate)
channel.delegate = self
// Add the channel to the current cast session
castSession?.add(channel)
// Send message on channel (the error parameter is a pointer at which to store the error result, this may be nil)
channel.sendTextMessage("message", error: nil)

Reference:

Receive data from the receiver

The general idea is to retrieve the current active cast session and add an event listener on the specific namespace.

Web SDK
// Retrieve the current cast session
var castSession = cast.framework.CastContext.getInstance().getCurrentSession();
// Add an event listener to the defined namespace channel
castSession.addMessageListener("namespace", (namespace, message) => {
console.log(namespace, message);
});

Reference:

Android SDK
// Retrieve the current cast session
CastSession castSession = CastContext.getSharedInstance(this).getSessionManager().getCurrentCastSession();
// Create callback handler
Cast.MessageReceivedCallback callback = new Cast.MessageReceivedCallback() {
@Override
public void onMessageReceived(CastDevice castDevice, String namespace, String message) {
// Do something with the message
}
};
// Add the message listener to the current cast session
castSession.setMessageReceivedCallbacks("namespace", callback);

Reference:

iOS/tvOS SDK and Legacy iOS/tvOS SDK (4.12.x)

The delegate of the channel will handle the receiving of messages.

extension ChromecastController : GCKGenericChannelDelegate {
func cast(_ channel: GCKGenericChannel, didReceiveTextMessage message: String, withNamespace protocolNamespace: String) {
print("Channel didReceiveTextMessage", message)
}
}

Reference:

Receive data on the receiver

This example is for receiver version 2.

The general idea is to retrieve the receiver manager and add a message listener for the specific namespace. This needs to be done before a THEOplayer instance is created. THEOplayer will call the start method of the receiverManager on initialization.

// Retrieve the cast receiver manager
var castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
// Retrieve the message bus from the cast receiver manager
var messageBus = castReceiverManager.getCastMessageBus("namespace");
// Add message listener to the message bus
messageBus.onMessage = function (event) {
console.log(event);
};

Reference:

Send data from the receiver

This example is for receiver version 2.

There are multiple possibilities to send data from the receiver:

  1. When a message is received on the receiver, you can answer the sender

  2. Broadcast to all connected senders

// 1.
messageBus.onMessage = function (event) {
messageBus.send(event.senderId, "message");
};
// 2.
messageBus.broadcast("message");

Reference: