Non-realtime WebSockets API
The Non-realtime WebSockets API allows you to establish persistent connections for AI requests without requiring repeated handshakes. This approach is ideal for applications that do not require real-time interactions but still benefit from reduced latency and continuous communication.
- Generate an AI Gateway token with appropriate AI Gateway Run and opt in to using an authenticated gateway.
- Modify your Universal Endpoint URL by replacing
https://withwss://to initiate a WebSocket connection:wss://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id} - Open a WebSocket connection authenticated with a Cloudflare token with the AI Gateway Run permission.
import WebSocket from "ws";
const ws