# Sending messages from/to Sender to/from Receiver on Web

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

## 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.

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

Reference:

* <https://developers.google.com/cast/docs/reference/chrome/cast.framework.CastSession>
* <https://developers.google.com/cast/docs/reference/chrome/cast.framework.CastSession#sendMessage>

## 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.

```js
// 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:

* <https://developers.google.com/cast/docs/reference/chrome/cast.framework.CastSession#addMessageListener>

## 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.**

```js
// 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:

* <https://developers.google.com/cast/docs/reference/receiver/cast.receiver.CastMessageBus>
* <https://developers.google.com/cast/docs/reference/receiver/cast.receiver.CastMessageBus#onMessage>
* <https://developers.google.com/cast/docs/reference/receiver/cast.receiver.CastMessageBus.Event>

## 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

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

Reference:

* <https://developers.google.com/cast/docs/reference/receiver/cast.receiver.CastMessageBus#broadcast>
* <https://developers.google.com/cast/docs/reference/receiver/cast.receiver.CastMessageBus#send>

## Related articles

* [Pass subtitle selection on to Chromecast](https://optiview.dolby.com/docs/theoplayer/how-to-guides/web/cast/chromecast/pass-subtitle-section-on-to-chromecast.md)

* [Chromecast SDK Setup](https://optiview.dolby.com/docs/theoplayer/getting-started/sdks/chromecast/getting-started.md)

* [How to configure a different stream to Chromecast](https://optiview.dolby.com/docs/theoplayer/how-to-guides/web/cast/chromecast/how-to-configure-to-a-different-stream.md)

* [Connecting from custom Sender applications](https://optiview.dolby.com/docs/theoplayer/how-to-guides/web/cast/chromecast/connecting-from-custom-sender-applications.md)
