Client-Side Session Handling and Layout

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

You will now connect backend and client behavior in one path: fetch credentials from your server, initialize the session, publish local media, and render remote streams.

The point of this module is to give you a clear operational map before the frontend exercise. You should know what each part does, where failures usually show up, and what “working” looks like.

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

These are the two client behaviors that matter most in the exercise.

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 either side breaks, the first debug step is to check whether credentials loaded correctly from your backend URL.

If you want deeper implementation details, use: