Skip to main content
In this tutorial, you’ll build a Serverless endpoint that streams data back to your client using WebSocket-style streaming. This approach works well for workloads that process data incrementally—like image processing or text generation—where you want to return partial results as they become available. You’ll create a handler that simulates chunked image processing, deploy it to Runpod Serverless, and build a Python client that receives streamed responses in real time.

What you’ll learn

  • Create a streaming handler function that yields incremental results.
  • Deploy a custom worker with streaming enabled.
  • Send requests to your endpoint and receive streamed responses.
  • Process and display streaming output in a Python client.

Requirements

Before starting, you’ll need:
  • A Runpod account with credits.
  • A Runpod API key.
  • Python 3.9+ installed locally.
  • Docker installed and configured.
  • A Docker Hub account for pushing your worker image.

Step 1: Set up your development environment

Create a project directory and set up a Python virtual environment:

Step 2: Create a streaming handler function

Create a file named handler.py with the following code:
handler.py
This handler:
  • Accepts a base64-encoded image in the request input.
  • Yields an initial status message with image metadata.
  • Processes the image in chunks, yielding progress updates for each chunk.
  • Sends a final completion message when done.
The key to streaming is using yield instead of return, and setting return_aggregate_stream to True when starting the serverless function. This makes the streamed results available via the /stream endpoint.

Step 3: Create a Dockerfile

Create a Dockerfile to package your handler:
Dockerfile
Create a requirements.txt file:
requirements.txt

Step 4: Build and push your Docker image

Build and push your image to Docker Hub:
Replace YOUR_DOCKERHUB_USERNAME with your actual Docker Hub username.

Step 5: Create a Serverless endpoint

Deploy your worker to a Serverless endpoint using the Runpod console:
  1. Go to the Serverless section of the Runpod console.
  2. Click New Endpoint.
  3. Click Import from Docker Registry.
  1. In the Container Image field, enter your Docker image URL (e.g., docker.io/YOUR_DOCKERHUB_USERNAME/runpod-base64-stream:latest).
  2. Click Next.
  1. Configure your endpoint:
    • Enter a name for your endpoint, or use the randomly generated name.
    • Make sure Endpoint Type is set to Queue.
    • Under GPU Configuration, select 16 GB GPUs.
    • Leave the rest of the settings at their defaults.
  1. Click Deploy Endpoint.
Once deployed, note the Endpoint ID from the endpoint details page—you’ll need it in the next step.

Step 6: Test the endpoint

Create a file named test_endpoint.py to test your streaming endpoint:
test_endpoint.py
Replace YOUR_RUNPOD_API_KEY and YOUR_ENDPOINT_ID with your actual API key and endpoint ID before running the script.
Run the test script:
You should see output similar to this:
The test script sends a base64-encoded image to your endpoint, then polls the /stream endpoint to receive incremental results as they become available.

Next steps

Now that you’ve built a streaming endpoint, explore these related topics: