# Working with streams

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Each Realtime Media Streams (RTMS) stream for a Zoom Phone call follows a predictable flow. It starts when a call connects, continues while your app connects to the required servers, receives stream data, and maintains the connection, and ends when the call ends or the stream stops.

Streams have the following states:

| State      |                                                                         |
| ---------- | ----------------------------------------------------------------------- |
| INACTIVE   | Default state. The session is not active yet.                           |
| INITIALIZE | The session is initializing.                                            |
| STARTED    | The session has started.                                                |
| PAUSED     | The session has been paused either by the consumer or the user.         |
| RESUMED    | The paused session has been resumed.                                    |
| STOPPED    | The session is being stopped by the consumer, user, or the call ending. |

## Prerequisites

To process RTMS data, your app needs the appropriate event subscriptions and scopes. For more information, see [Adding RTMS to your app](/docs/rtms/phone/add-features/).

Once your app is configured, RTMS streams follow these steps:

## Step 1: RTMS is started

Phone RTMS streams start automatically when a call starts. An admin turns this on for each user in the phone system policy settings, and the user can stop the stream during the call if you turn on user controls.

> **Note**
>
> Your account has a concurrent stream limit based on your Developer Pack subscription. Each RTMS session ID counts as one concurrent stream. For more information, see [Concurrent stream limits](/docs/rtms/troubleshooting/#concurrent-stream-limits).

The streaming experience is controlled by the following settings. An admin chooses these settings for each user when turning on RTMS. For more information, see [Turn on RTMS for phone users](/docs/rtms/phone/add-features/#turn-on-rtms-for-phone-users).

-   **Automatic start** starts RTMS as soon as a phone call begins, and ends RTMS when the call ends.
-   **User controls** let the user stop RTMS during a call from the Zoom client.
-   **Audio notifications** play a recorded message to inform the callee that the call is being recorded in real time.

## Step 2: App receives streaming notification

Zoom sends [phone.call_rtms_started](/docs/api/rtms/events/) webhook events when RTMS streaming starts. You can then use the information in the event to establish a signaling connection in the next step. The `server_urls` value can contain one URL for each media protocol, separated by commas, such as `wss://` and `srt://`.

To receive notifications when new streams are available, create an HTTP POST handler in your web app. This handler acts as the endpoint for incoming webhook events. In your app settings, provide the URL of this endpoint. For more information, see [Subscribe to RTMS events](/docs/rtms/phone/add-features/#subscribe-to-rtms-events).

After you receive an event, [verify the event's signature](/docs/api/webhooks/#verify-webhook-events) to ensure it's from a trusted source.

Store the `server_urls` value from the `phone.call_rtms_started` event for the stream. The [phone.call_rtms_interrupted](/docs/api/rtms/events/) event doesn't include `server_urls`, so you need the stored value if you reconnect. For more information, see [Failover and reconnection](/docs/rtms/phone/failover-reconnection/).

## Step 3: App establishes signaling connection

Establishing a signaling connection to an RTMS server enables your app to establish and manage a WebSocket connection with the RTMS server. It begins with a signed handshake and includes messages for session readiness, state updates, and stream control.

The signaling connection provides lifecycle updates for the media connections, such as when they start, stop, or encounter an interruption event.

When you have the connection details and know a stream is available, you can start the connection to the signaling server. Split the `server_urls` value on commas, and select the `wss://` URL, because an `srt://` URL can't be used for signaling.

1. Create a signature that your app uses to securely connect to the RTMS server. Replace `client_id` and `secret` with your app's **Client ID** and **Client Secret**, and `call_id` and `rtms_stream_id` with the **call_id** and **rtms_stream_id** from the streaming notification event.

`HMACSHA256(client_id + "," + call_id + "," + rtms_stream_id, secret);`

2. Your app sends a [signaling handshake request](/docs/rtms/event-reference/universal-events/#signaling-handshake-request) with the `call_id` and `rtms_stream_id` from the streaming notification event, the signature you just created, and `protocol_version` set to `2`. If the handshake is successful, your app receives a [signaling handshake response](/docs/rtms/event-reference/universal-events/#signaling-handshake-response) confirming the connection and containing the media server locations.

> If the handshake fails, the server responds with a `SIGNALING_HAND_SHAKE_RESP` message containing a status code and reason.

Once your app has successfully established a signaling connection, it can establish the media connections.

## Step 4: App establishes the media connections

For each media type, split its value in `media_server.server_urls` on commas. If you use protocol V2, a value can include an `srt://` URL as well as a `wss://` URL. Select the `wss://` URL to connect over WebSockets, or the `srt://` URL to connect over SRT. For SRT setup, see [Connecting over SRT](/docs/rtms/srt/connecting-over-srt/).

Use the URLs listed in `media_server.server_urls` in the [signaling handshake response](/docs/rtms/event-reference/universal-events/#signaling-handshake-response) to establish media WebSocket connections. Zoom Phone streams provide two media types: audio and transcript. Open one media connection for each type.

> **Use integers for message and event types**
>
> For [data type](/docs/rtms/data-types/) definitions, use the representative enum integers.
>
> Example: Send `msg_type: 1` ✓ not `msg_type: SIGNALING_HAND_SHAKE_REQ` ✗

1. Your app sends a [media handshake request](/docs/rtms/event-reference/universal-events/#media-handshake-request) with the `call_id` and `rtms_stream_id` from the streaming notification, the signature you created in [Step 3](/docs/rtms/phone/work-with-streams/#step-3-app-establishes-signaling-connection), and `media_type` set to `1` for audio or `8` for transcript. If the handshake is successful, your app receives a [media handshake response](/docs/rtms/event-reference/universal-events/#media-handshake-response) confirming the connection.

> If the handshake fails, the server responds with a `SIGNALING_HAND_SHAKE_RESP` message containing a status code and reason.

2. Your app sends a [client ready ACK message](/docs/rtms/event-reference/universal-events/#client-ready-ack-message) to the signaling connection to indicate readiness to receive media.

Now that the connections are made, your app can receive media data.

## Step 5: App receives media data

Once the connections are established, your app receives continuous streams of:

-   [Audio data](/docs/rtms/event-reference/universal-events/#audio-data) from call participants
-   [Transcript data](/docs/rtms/event-reference/universal-events/#transcript-data) from call participants

For more information about working with media data, see [Handling media data](/docs/rtms/phone/media/).

Your app also receives call events during the stream, such as when participants join or leave, or when the call is held, resumed, or transferred. For the full list, see [Zoom Phone events](/docs/rtms/event-reference/phone-events/).

## Step 6: App maintains connections

Throughout the stream, your app must:

-   Respond to [keep-alive requests](/docs/rtms/event-reference/universal-events/#keep-alive-request) to maintain stable connections (sent every 10 seconds when no data is flowing)
-   Monitor [session state updates](/docs/rtms/event-reference/universal-events/#session-state-updated) for pauses, resumes, or interruptions
-   Handle [stream state changes](/docs/rtms/event-reference/universal-events/#stream-state-updated) for connection issues or termination

> **Connection maintenance is critical:** If your app fails to respond to three consecutive keep-alive requests (sent every 10 seconds during idle periods), the RTMS server terminates the connection.

If a connection is interrupted, see [Failover and reconnection](/docs/rtms/phone/failover-reconnection/) for more information on reestablishing the connection.

## Step 7: Stream ends

The RTMS stream ends when:

-   The call ends
-   The user stops streaming (`stop_reason` `2`)
-   A connection error ends the stream (`stop_reason` `10`)

Your app receives a `phone.call_rtms_stopped` notification indicating the stream has ended. The `stop_reason` field explains why. For the possible values, see [RTMS_STOP_REASON](/docs/rtms/data-types/#rtms_stop_reason).
