Client-Side Session Handling and Layout

With a backend URL ready, this module shifts to the browser side of the same flow.

In this module, we move from backend readiness to client behavior in the browser. First, you will see how the app uses your backend URL to fetch session credentials and initialize the call. Then we will cover the two core media actions: publishing the local participant and subscribing to remote participant media. By the end, you will have a clear high-level map of what needs to work before you start the coding exercise.

What changes on the client side

  • The app requests credentials from your backend URL.
  • The client initializes the session and connects participants.
  • Local media is published, and remote media is rendered in the layout.

Key concepts to keep in mind

Publish flow (local participant)

Once you have a Publisher object and a session, the next step is to pass it to the session object to begin streaming.

To publish a stream, add an OTPublisher component as a child of the OTSession object:

<OTSession
  applicationId="your-application-id"
  sessionId="the session ID"
  token="the token">
  <OTPublisher/>
</OTSession>

The publisher starts streaming when the client connects to the session. The OTPublisher object dispatches a streamCreated event when it starts streaming to the session. It dispatches an error event if there is an error publishing. Set an eventHandlers prop of the OTPublisher component, and set the streamCreated and error properties of that object to callback functions:

<OTPublisher
eventHandlers={{
  streamCreated: () => {
    console.log('The publisher started streaming.');
  },
  error: event => {
    console.log('Publisher error:', event);
  },
}}/>

Subscribe flow (remote participant)

Use the session to subscribe to a remote stream and render it in your UI.

To subscribe to all streams in the session, add an OTSubscriber object as a chile of the OTSession object:

<OTSession
  applicationId="the Application ID"
  sessionId="the session ID"
  token="the token">
  <OTSubscriber/>
</OTSession>

After the client connects to the session, the OTSubscriber object adds views for subscriber videos when other clients streams become available in the session.

The OTSubscriber object dispatches a connected event when a subscriber successfully starts streaming. It dispatches an error event if there is an error subscribing. Set an eventHandlers prop of the OTSubscriber component, and set the connected and error properties of that object to callback functions:

<OTSubscriber
eventHandlers={{
  connected: () => {
    console.log('The subscriber started streaming.');
  },
  error: () => {
    console.log('The subscriber failed.');
  }
}}/>

The key idea is simple: local media must publish cleanly, and remote media must subscribe and render predictably.

If you want deeper implementation details, use: