WebSocket Support Launches on Modal for Real-Time Application Development
- Gate
- Machine Learning
- Departs
- Crew
- Christopher Davis
- Passengers
- 965
Modal now supports WebSocket connections, enabling developers to build real-time applications with automatic scaling capabilities.

Modal has recently introduced support for WebSocket connections, enhancing the platform’s capabilities for developers focused on real-time applications. WebSockets provide a persistent, full-duplex communication channel between client and server, distinguishing it from traditional HTTP, which operates on a request/response model that opens and closes connections for each exchange. This shift is significant because real-time applications require instantaneous data exchange, a domain where traditional protocols fall short.
Setting Up a WebSocket Server on Modal
Integrating WebSockets into Modal is straightforward. Developers can use any library compatible with their chosen programming environment to initiate a WebSocket connection. Here’s a code sample using FastAPI, a popular web framework for building APIs:
import modal
image = modal.Image.debian_slim().pip_install("fastapi", "websockets")
app = modal.App("my-app", image=image)
@app.function()
@modal.asgi_app()
def endpoint():
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket("/ws")
async def websocket_handler(websocket: WebSocket) -> None:
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
return app
After saving the above code in a file named main.py, you can deploy it using the command modal deploy main.py. Modal will treat each WebSocket connection as a separate piece of input. Therefore, it's advisable to enable concurrent input handling, especially for functions that they’re not heavily reliant on CPU or GPU resources. If this is not configured, Modal will create a new container for each WebSocket connection, which could lead to inefficiencies in resource utilization.
Benefits of Using WebSockets on Modal
One of the key benefits of utilizing WebSockets on Modal is the platform's automatic scaling feature. This applies explicitly to WebSocket handlers, making it easier for developers to create responsive applications that can accommodate fluctuating user demands. For instance, a real-time speech-to-text application could deploy both the WebSocket server and the model for transcription as separate Modal Functions. As traffic increases, Modal handles the scaling of containers transparently, allowing developers to concentrate on application logic rather than infrastructure concerns. This is more significant than it looks; by offloading scaling responsibilities, developers can devote more time to refining their projects.

Use Cases for WebSocket Implementation
The addition of WebSocket support on Modal opens up numerous possibilities for application development. Here are some key use cases highlighted by users:
1. Real-Time Streaming Responses
WebSockets are particularly beneficial for applications requiring real-time data transfer, such as audio streaming services. In a scenario involving a speech-to-text application that uses the Whisper model, WebSockets allow for continuous, live transcription as audio flows in. This capability extends to other scenarios like text-to-speech or text-to-image applications, where timely responses are crucial for user experience. Users expect immediate feedback, and anything less could disrupt engagement. This immediacy isn't just a technical advantage; it fundamentally shifts how users interact with applications, creating a more engaging experience.
2. Progress Updates for Long-Running Tasks
WebSockets also excel in scenarios involving lengthy processing tasks, such as using large language models (LLMs) to analyze comprehensive data sets. In such instances, sending regular status updates back to the client proves beneficial for maintaining user engagement. The persistent connection enabled by WebSockets allows servers to push these progress indicators in real-time, enhancing the interaction during long jobs. Users appreciate transparency regarding wait times, and consistent updates can significantly improve overall satisfaction.
3. Deploying Popular Open-Source Frameworks
Many existing open-source frameworks, such as ComfyUI, Streamlit, and Gradio, require WebSocket connections for deployment. Modal’s environment supports users in running these frameworks efficiently. For instance, users can leverage ComfyUI to design stable diffusion pipelines or employ Streamlit and Gradio for rapid prototyping of new machine learning features or mini-apps. The interaction and real-time updates provided by WebSockets are crucial for creating responsive user interfaces and visualizations in these frameworks. (And this is the part most people overlook: the capacity for real-time interaction can dramatically influence the usability of machine learning applications.)
For further information on implementing these frameworks, you can explore our examples of running Streamlit on Modal. This guidance will assist developers in making the most out of the newly introduced WebSocket functionality.
Implications of WebSocket Support on Modal
As Modal integrates features like WebSocket support, it's signaling a commitment to addressing developers' escalating requirements, particularly in building scalable and interactive applications. If you're working in this space, you might appreciate the potential to create more immersive and responsive user experiences as a result. WebSockets are not just a technical enhancement; they represent a shift in how developers can think about real-time engagement.
This change also implies that more and more applications will likely shift toward real-time interactivity across the board. The implications are clear: as users become accustomed to seamless, instant communication in one area, their expectations will naturally rise in others. As Modal expands its capabilities, its developers could be setting the stage for a significant transformation in application user engagement, one that might influence the features and tools that competitors feel pressured to adopt.
This trajectory reflects broader industry trends where almost every sector seems to be moving toward increasing immediacy in communication. As developers tap into these capabilities, the focus will shift towards making applications more intuitive and user-friendly, capitalizing on the advantages that WebSockets bring to the table.