---
sidebar_position: 7
sidebar_label: Live streams
---

# Live streams in FastAPI

One person presents live to everyone watching: the picture, a chat beside it, reactions, and the count of who watches. The host goes live from the browser, a phone or OBS. Viewers may be signed in or not.

## Who is who

Your server declares streams beside its rooms and calls, by pattern. The rule says who each person is in the stream:

```python
# app.py: the show of the shop, show:mugs. Ann presents it, and everyone else watches and writes in its chat, signed in or not.
def show_role(user, p):
    if p["id"] != "mugs":
        return False
    return "host" if user == ANN else "viewer"

realtime.streams("show:{id}", show_role, reactions=True)
```

- **The answer:** `False`, `"host"` or `"viewer"`, or a dict: `role`, `info` (what others see of them), `chat` (`write` or `read`), `mod` (keeps the chat), `src` and `quality` (what the host sends).
- **A visitor who is not signed in** comes with `user` None, and may only be a viewer.
- **The settings of the stream**, keyword arguments: `reactions`, `people` and `chat` (`offline`, `links`, `fresh`), each a value or a function of the parts of the name ([live streams in depth](../apps/streams.md#who-is-who)).

The calls route (`/api/realtime/calls`) is already among the answers of `wr.router()`, for streams too.

## The page

```js
const calls = await rt.calls();
const show = await calls.stream( 'show:mugs' );

show.watch( document.querySelector( '#show video' ) );      // the picture and its sound
show.on( 'state', ( state ) => draw( state ) );            // idle, live, ended
show.on( 'viewers', ( n ) => ( count.textContent = `${ n } watching` ) );
show.on( 'message', ( m ) => addLine( m.from.name, m.text ) );
await show.chat.send( 'Hello!' );

if ( show.role === 'host' ) {
    await show.preview();    // the camera, shown in the same video
    await show.goLive();
}
```

The show card of the Example Shop does exactly this, in `showCard()` of its page.

## More

Reactions, keeping the chat, OBS, and the refusals: [live streams in depth](../apps/streams.md).
