DO it again: how we used Durable Objects to add WebSockets support and authentication to AI Gateway
Nov 19, 2024Catarina Pires Mota, Gabriel Massadas

Summary
This post details the implementation of WebSocket support and authentication for Cloudflare's AI Gateway. It describes how Durable Objects are used to manage persistent WebSocket connections, handle authentication via API tokens (including a fallback for browser limitations using the 'sec-websocket-protocol' header), and route requests to various AI providers. The post also introduces the `eventId` field for correlating asynchronous messages in streaming scenarios and provides code examples for both Node.js and browser WebSocket clients.
More from the Artificial Intelligence Integration & Impact trail
- Introducing Clef-omni with full multimodality, plus a faster Clef and a cheaper Clef-flash
- Introducing Web Search API via AI Gateway
- Introducing Clef: our open-source decision models, and new RL fine-tuning platform
- One year later: Sovereign AI and the fight for choice
- AI Search is now generally available