Websocket Endpoints
Using a WebSocket endpoint allows responses to be streamed to the client, enabling real-time communication and efficient data transfer.
Warning: This functionality is in beta and may change in the future.
Required changes
To set up a WebSocket endpoint, you need to configure your app to use a custom runtime.
Below is an example of the required changes in your cerebrium.toml
configuration file:
Explanation:
- port: The port your app will listen on inside the container.
- entrypoint: The command to start your app. In this example, we’re using Uvicorn to run a FastAPI app located in main.py.
- healthcheck_endpoint: The endpoint used by Cerebrium to verify that your app is running.
Things to note
-
Custom Runtime Required: A custom runtime is necessary to set up a WebSocket endpoint because it allows you to define how your application is run inside the container.
-
WebSocket URL: Requests need to be made to a WebSocket URL starting with wss://. Ensure that your client supports secure WebSocket connections.
Making a request
You can test your WebSocket endpoint using websocat, a command-line utility for connecting to WebSocket servers:
Implementing the WebSocket Endpoint
Here’s an example of how you can implement a WebSocket endpoint using FastAPI:
Additional Info
Client-Side Implementation: When connecting from a client app, ensure you handle the WebSocket connection properly, including error handling and reconnection logic if necessary.