DO it again: how we used Durable Objects to add WebSockets support and authentication to AI Gateway
We've introduced a WebSockets API that establishes a single, persistent connection to the AI Gateway, enabling continuous communication and reducing the overhead of repeated handshakes and TLS negotiations. This API allows developers to access all AI providers supported by AI Gateway via WebSocket, with the option to authenticate requests using Cloudflare API tokens or custom headers in browser WebSocket clients. The new API uses Durable Objects to handle all messages for the connection, storing and applying AI Gateway settings passed via headers on a per-request basis. We've also introduced authentication for the AI Gateway WebSockets API, requiring a valid token included in the cf-aig-authorization header. This allows developers to maintain a single TCP connection between their client or server application and the AI Gateway, while ensuring security and scalability. The new API is now in beta and open to everyone, with support for streaming requests and handling asynchronous events through an eventId field.
Company
Cloudflare
Date published
Nov. 19, 2024
Author(s)
Catarina Pires Mota, Gabriel Massadas
Word count
1433
Language
English
Hacker News points
None found.