> ## Documentation Index
> Fetch the complete documentation index at: https://docs.radion.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Realtime

> Connect to the Radion realtime API with radion.realtime.

`radion.realtime` is the WebSocket client. It runs the whole connection for you. It connects, reconnects after an unexpected drop, puts your subscriptions back, and sends each incoming frame to the handlers you set up.

## Connect

`connect()` opens the socket and finishes once the connection is up. If the first try fails, it errors.

<CodeGroup>
  ```typescript TypeScript theme={null}
  const radion = new Radion({ apiKey: process.env.RADION_API_KEY });

  await radion.realtime.connect();
  radion.realtime.subscribe({ id: "trading", channel: "trading" });
  radion.realtime.onChannel("trading", (event) => console.log(event.data));
  ```

  ```python Python theme={null}
  radion = Radion(api_key="sk_...")

  await radion.realtime.connect()
  await radion.realtime.subscribe(Subscription(id="trading", channel="trading"))

  @radion.realtime.on_channel("trading")
  async def handle(event):
      print(event.data)
  ```

  ```rust Rust theme={null}
  use futures_util::StreamExt;
  use radion_sdk::realtime::{Channel, Subscription};

  let radion = Radion::builder().api_key("sk_...").build()?;

  radion.realtime.connect().await?;
  let mut trading = radion
      .realtime
      .subscribe(Subscription::new("trading", Channel::Trading))
      .await?;
  while let Some(event) = trading.next().await {
      println!("{:?}", event.data);
  }
  ```
</CodeGroup>

The client just remembers what you want. You can call `subscribe` before or after `connect`. Either way, the client sends your subscriptions once the socket is open.

## Lifecycle

1. **Connect** — `connect()` opens the socket with your `X-API-Key` header.
2. **Subscribe** — add your subscriptions. The client sends them on open and again on every reconnect.
3. **Receive** — channel events go to your handlers. Lifecycle events tell you the connection state.
4. **Close** — `close()` shuts down cleanly and stops all reconnect tries.

## Reference

<CardGroup cols={2}>
  <Card title="Subscriptions" icon="satellite-dish" href="/sdks/realtime/subscriptions">
    Subscribe with ids and filters.
  </Card>

  <Card title="Events" icon="bell" href="/sdks/realtime/events">
    Channel, wildcard, and lifecycle handlers.
  </Card>

  <Card title="Reconnect & heartbeats" icon="plug" href="/sdks/realtime/reconnect">
    Backoff, keep-alive, and resubscribe.
  </Card>

  <Card title="Errors" icon="triangle-alert" href="/sdks/realtime/errors">
    The error hierarchy and server codes.
  </Card>
</CardGroup>
