Skip to main content
WebSocket endpoints stream responses to the client, enabling real-time, bidirectional communication.

Required changes

Setting up a WebSocket endpoint requires a custom runtime. Configure it in cerebrium.toml:
Fields:
  • port: The port the app listens on inside the container.
  • entrypoint: The command to start the app. This example uses Uvicorn to run a FastAPI app in main.py.
  • healthcheck_endpoint: Confirms instance health. Defaults to a TCP ping on the configured port. A non-200 response marks the instance as unhealthy, triggering a restart if it does not recover.
  • readycheck_endpoint: Confirms the instance is ready to receive traffic. Defaults to a TCP ping on the configured port. A non-200 response removes the instance from request routing.

Things to note

  • Custom Runtime Required: WebSocket endpoints require a custom runtime to control how the app runs inside the container.
  • WebSocket URL: Requests must use a wss:// URL. The client must support secure WebSocket connections.

Making a request

Test the WebSocket endpoint using websocat, a command-line WebSocket client:

Implementing the WebSocket Endpoint

Example WebSocket endpoint using FastAPI:

Additional Info

Client-side Implementation: Handle the WebSocket connection properly on the client, including error handling and reconnection logic.