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

# Realtime and Streaming

## Deploying a Realtime App

This is a simple example of a realtime streaming app. When deployed, this app will be exposed as a public websocket endpoint.

The `realtime` handler accepts a single parameter, called `event`, with the event payload.

<Tip>
  The `realtime` decorator is an abstraction above `asgi`.

  This means that additional parameters in `asgi`, such as [`concurrent_requests`](/v2/endpoint/web-server#concurrent-requests) can be used too.
</Tip>

```python app.py theme={null}
from beam import realtime


@realtime(
    cpu=1,
    memory="1Gi",
    concurrent_requests=10, # Process 10 requests at a time 
    authorized=False, # Don't require auth to invoke 
)
def stream(event):
    # Echo back the event payload sent to the websocket
    return {"response": event}
```

This app can be deployed in traditional Beam fashion:

```sh theme={null}
beam deploy app.py:stream
```

## Streaming Responses from the Client

Realtime Endpoints can be connected to from any websocket client.

<Tabs>
  <Tab title="Beam Javascript SDK">
    The code below uses the Beam Javascript SDK to send requests to the realtime app.

    Make sure to add an `.env` file to your project with your `BEAM_DEPLOYMENT_ID` and `BEAM_TOKEN`:

    ```javascript client.js theme={null}
    import beam from "@beamcloud/beam-js";

    const streamResponse = async () => {
      const client = await beam.init(process.env.BEAM_TOKEN);
      const deployment = await client.deployments.get({ id: process.env.BEAM_DEPLOYMENT_ID });

      const connection = await deployment.realtime();
      
      const payload = {
        "event": "Echo this back",
      }

      connection.onmessage = (message) => {
          console.log(`🎉 Response: ${message.data}`);
      };

      connection.send(JSON.stringify(payload));

      setTimeout(() => {
        connection.close();
      }, 1000);
    };

    streamResponse();
    ```
  </Tab>

  <Tab title="Javascript">
    The code below uses the native WebSocket API to send requests to the realtime app.

    ```javascript client.js theme={null}
    const socket = new WebSocket("wss://1c0f0cbe-e0d1-49ae-a556-5daffe23eb4c.app.beam.cloud");

    // Connection opened
    socket.addEventListener("open", (event) => {
      socket.send("Hello Server!");
    });

    // Listen for messages
    socket.addEventListener("message", (event) => {
      console.log(event.data); // {"response":"Hello Server!"}
    });
    ```
  </Tab>
</Tabs>
