From e470e0c6e3c2592d9359d7a0aa8923d56e4c4abe Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sun, 15 Dec 2024 06:58:05 +0000 Subject: [PATCH] update docs --- .../components/documentation/Navigation.jsx | 4 ++ .../documentation/api_reference/streaming.mdx | 61 ++++++++++++++----- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/docs_src/src/components/documentation/Navigation.jsx b/docs_src/src/components/documentation/Navigation.jsx index 2ad4b7225..2a1e8704a 100644 --- a/docs_src/src/components/documentation/Navigation.jsx +++ b/docs_src/src/components/documentation/Navigation.jsx @@ -289,6 +289,10 @@ export const navigation = [ href: '/documentation/api_reference/exceptions', title: 'Exceptions', }, + { + href: '/documentation/api_reference/streaming', + title: 'Streaming', + }, { href: '/documentation/api_reference/scaling', title: 'Scaling the Application', diff --git a/docs_src/src/pages/documentation/api_reference/streaming.mdx b/docs_src/src/pages/documentation/api_reference/streaming.mdx index 0971a5787..7e9b0aaee 100644 --- a/docs_src/src/pages/documentation/api_reference/streaming.mdx +++ b/docs_src/src/pages/documentation/api_reference/streaming.mdx @@ -1,15 +1,28 @@ +--- +title: 'Streaming' +description: 'Stream data efficiently with Robyn' +--- + +import { Row, CodeGroup, Steps, Callout } from '@/components/mdx' + +## Coming From + +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. + ## Streaming Responses 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. -Streaming responses are perfect for handling large datasets or real-time updates without consuming excessive memory. + + Streaming responses are perfect for handling large datasets or real-time updates without consuming excessive memory. + ## Response 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: - + ```python from robyn import Response @@ -28,7 +41,7 @@ When the Bat-Signal needs to stream continuously through the night sky, you'll w ``` - + ```bash curl http://localhost:8000/bat-signal ``` @@ -48,25 +61,25 @@ When the Bat-Signal needs to stream continuously through the night sky, you'll w Like Batman's versatile arsenal, the streaming response system supports multiple data types: - + ```python # Raw binary data (like Batcomputer logs) yield b"Batcomputer Log Entry\n" ``` - + ```python # Text messages (like Alfred's updates) yield "Master Wayne, your tea is ready\n".encode() ``` - + ```python # Numbers (like Batmobile telemetry) yield str(speed).encode() ``` - + ```python # JSON data (like Gotham City surveillance) yield json.dumps({"location": "Crime Alley"}).encode() @@ -79,7 +92,7 @@ Like Batman's versatile arsenal, the streaming response system supports multiple For real-time updates from the Batcomputer: - + ```python @app.get("/batcomputer/events") async def batcomputer_feed(): @@ -103,7 +116,7 @@ For real-time updates from the Batcomputer: ) ``` - + ```javascript const evtSource = new EventSource("/batcomputer/events"); evtSource.onmessage = (event) => { @@ -118,7 +131,7 @@ For real-time updates from the Batcomputer: For streaming large files from the Batcomputer archives: - + ```python @app.get("/batcomputer/files") async def download_files(): @@ -138,7 +151,7 @@ For streaming large files from the Batcomputer archives: ) ``` - + ```bash curl -O http://localhost:8000/batcomputer/files ``` @@ -204,9 +217,29 @@ async def test_bat_signal(): assert len(signals) > 0 ``` -## What's next? +## Best Practices + + +### Encode Data +Always encode strings to bytes (like encrypting Bat-communications) + +### Chunk Size +Use appropriate chunk sizes (8KB-64KB for efficient data transfer) + +### Resource Management +Clean up resources (like Batman cleaning up Gotham) + +### Memory Usage +Don't accumulate data in memory (keep the Batcomputer running smoothly) + +### Timeouts +Implement timeouts (even Batman needs sleep) + + +## What's Next? -Now, Batman wanted to scale his application across multiple cores. Robyn led him to Scaling. +Now that you've mastered streaming, you might want to explore: -- [Scaling](/documentation/api_reference/scaling) +- [WebSockets](/documentation/api_reference/websockets) - For real-time bidirectional communication +- [Scaling](/documentation/api_reference/scaling) - Scale your streaming applications across multiple cores