Brendan McKeag

Intro to WebSocket Streaming with RunPod Serverless

February 19, 2025

In this followup to our 'Hello World' tutorial, we'll create a serverless endpoint that processes base64-encoded files and streams back the results. This will demonstrate how you can work with file input/output over our serverless environment by encoding the file as data within a JSON payload.

Prerequisites

As before, this tutorial will be aimed at OSX developers.

Step 1: Creating the Project

Create your development environment.

Step 2: Creating the Handler

Create a new file called handler.py. Remember that the handler loop is how code gets executed when a worker is active. In this example, this handler will simulate image processing. Since this tutorial is to demonstrate the serverless environment more than process images, we will have it just create a static, blank image as the payload.

Step 3: Creating the Dockerfile and requirements.txt

As with the previous tutorial, we'll need to provide the Dockerfile and requirements.txt to build and push the image.

Step 4: Build and Push to DockerHub

As before, build and push your image to DockerHub, and then pull it into your endpoint.

Step 5: Running the Endpoint in Code

Here, we'll provide an example of how to interact with the endpoint in code. You'll need to provide your RunPod API key and Endpoint ID in the variables up top. Let's call this test_endpoint.py.

What this code will do is send a request to the endpoint you've created, let it process and return base64 data in a JSON payload, and return it to your script for further local processing and saving.

Run the test:

python test_endpoint.py

You should see output like this, along with a base64 JSON payload saved in the folder you ran the script in.

Conclusion

You've now learned how to create a RunPod serverless endpoint that can process base64-encoded files and stream results back to the client. This pattern can be extended to handle various types of file processing tasks while providing real-time feedback to users.

Build what’s next.

The most cost-effective platform for building, training, and scaling machine learning models—ready when you are.

12:22