You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs_src/src/pages/documentation/api_reference/streaming.mdx
+77-30
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,20 @@
1
1
exportconst description =
2
-
'On this page, we’ll dive into the different conversation endpoints you can use to manage conversations programmatically.'
2
+
'Learn how to use streaming responses in Robyn for real-time data, large files, and server-sent events.'
3
3
4
-
5
-
6
-
## Coming From
7
-
8
-
If you're coming from [File Handling](/documentation/api_reference/file_handling), you'll find streaming provides a more efficient way to handle large files.
9
-
10
-
## Streaming Responses
4
+
## Overview
11
5
12
6
Like Batman's gadgets streaming from the Batcave to his utility belt, Robyn provides built-in support for streaming responses. This allows you to send data in chunks, perfect for large files, real-time updates, and server-sent events.
13
7
14
-
Streaming responses are perfect for handling large datasets or real-time updates without consuming excessive memory.
8
+
## Creating Streaming Responses
15
9
16
-
## Response
10
+
There are two ways to create streaming responses in Robyn:
17
11
18
-
When the Bat-Signal needs to stream continuously through the night sky, you'll want to use a generator or iterator as the `description`parameter:
12
+
### 1. Using the streaming parameter
19
13
20
14
<Row>
21
15
<CodeGrouptitle="Server">
22
16
```python
23
-
from robyn import Response
24
-
25
-
@app.get("/bat-signal")
17
+
@app.get("/bat-signal", streaming=True)
26
18
asyncdefstream_signal():
27
19
asyncdefsignal_generator():
28
20
whileTrue:
@@ -44,13 +36,46 @@ When the Bat-Signal needs to stream continuously through the night sky, you'll w
44
36
</CodeGroup>
45
37
</Row>
46
38
39
+
### 2. Returning an Iterator/Generator
40
+
41
+
Robyn automatically detects iterators and generators and treats them as streaming responses:
42
+
43
+
<Row>
44
+
<CodeGrouptitle="Server">
45
+
```python
46
+
@app.get("/bat-signal")
47
+
asyncdefstream_signal():
48
+
asyncdefsignal_generator():
49
+
whileTrue:
50
+
yieldb"Bat-Signal Active\n"
51
+
await asyncio.sleep(1)
52
+
53
+
return signal_generator() # Automatically detected as streaming
54
+
```
55
+
</CodeGroup>
56
+
</Row>
57
+
58
+
## Response Object
59
+
60
+
The Response class supports streaming through its constructor parameters:
61
+
62
+
```python
63
+
Response(
64
+
status_code=200,
65
+
headers={"Content-Type": "text/plain"},
66
+
description=generator(), # Can be str, bytes, or iterator/generator
67
+
streaming=True# Optional, automatically set for iterators/generators
68
+
)
69
+
```
70
+
47
71
### Parameters
48
72
49
73
| Name | Type | Description | Default |
50
74
|------|------|-------------|---------|
51
75
| status_code | int | Response status code | 200 |
0 commit comments