When to Use Realtime vs Streaming
| Feature | Realtime (WebSocket) | Streaming (SSE) |
|---|---|---|
| Direction | Bidirectional (client ↔ server) | One-way (server → client) |
| Connection | Persistent, reusable | New connection per request |
| Latency | Lower (connection reuse) | Higher (new connection each time) |
| Best for | Interactive apps, back-to-back requests | Progressive output, previews |
| Protocol | Binary msgpack | JSON over SSE |
- Users send multiple requests in quick succession (e.g., interactive image editing)
- You need the lowest possible latency between requests
- You’re building interactive, back-and-forth experiences
- You want to show progressive output (e.g., image generation previews, video updates)
- Clients send a single request and receive multiple updates
- You don’t need bidirectional communication
How Realtime Works
Under afal.App, the @fal.realtime() decorator makes your endpoint compatible with fal’s real-time clients. It uses fal’s binary msgpack protocol for efficient serialization and eliminates connection establishing overhead for repeated requests.
Important: The
fal_client.realtime() method automatically connects to the /realtime path on your app. If you use @fal.realtime(), you must set the path to /realtime (e.g., @fal.realtime("/realtime")) for the client to connect successfully.@fal.endpoint can
be initialized with is_websocket=True flag and the underlying function will receive the raw WebSocket connection and
can choose to use it however it wants.
Server-Side Implementation
Here’s an example of a fal app with both a regular HTTP endpoint and a WebSocket endpoint:Client-Side Connection
Connecting to @fal.realtime() Endpoints
For endpoints decorated with @fal.realtime(), use fal_client.realtime() or fal_client.realtime_async(). These methods handle serialization automatically using fal’s binary protocol:
Connecting to Raw WebSocket Endpoints
For endpoints usingis_websocket=True, use fal_client.ws_connect() or fal_client.ws_connect_async() for direct WebSocket access:
- For
@fal.realtime()endpoints: Usefal_client.realtime()- serialization is handled automatically. - For raw
is_websocket=Trueendpoints: Usefal_client.ws_connect()with thepathparameter to specify the endpoint path.