DO it again: how we used Durable Objects to add WebSockets support and authentication to AI Gateway

Catarina Pires Mota
8 min readadvanced
--
View Original

Overview

The article discusses how Cloudflare utilized Durable Objects to implement WebSockets support and authentication for the AI Gateway. It highlights the benefits of maintaining persistent connections for efficient communication and introduces a new WebSockets API that simplifies the integration of AI providers.

What You'll Learn

1

How to establish a WebSocket connection to AI Gateway using Cloudflare's API

2

Why using Durable Objects enhances the scalability of WebSocket connections

3

How to implement authentication for WebSocket connections using Cloudflare API tokens

Prerequisites & Requirements

  • Basic understanding of WebSocket protocols and Cloudflare's Developer Platform
  • Familiarity with Cloudflare API and Durable Objects(optional)

Key Questions Answered

How does the new WebSockets API improve communication with AI Gateway?
The new WebSockets API allows for a single, persistent TCP connection between client applications and the AI Gateway. This reduces the overhead of repeated handshakes and TLS negotiations, enabling continuous communication and efficient request handling, even for providers that do not support WebSockets natively.
What are the authentication requirements for connecting to AI Gateway via WebSocket?
To connect to AI Gateway using WebSocket, developers must create a Cloudflare API token with the permission 'AI Gateway: Run' and include this token in the 'cf-aig-authorization' header of their requests. This ensures that only authenticated requests are processed by the gateway.
What happens if a WebSocket connection does not include the required authentication token?
If the authentication token is missing from a request when the Authenticated Gateway is enabled, the request will fail. This mechanism ensures that only verified requests can access the AI Gateway, enhancing security.
How does the Durable Object handle multiple streaming requests?
The Durable Object can manage multiple streaming requests simultaneously by using a new field called 'eventId'. This allows the server to stream responses in parallel while enabling the client to identify which response corresponds to each request based on the eventId.

Key Statistics & Figures

Logs processed by AI Gateway
Over 3 billion
This statistic highlights the scale at which the AI Gateway operates and the necessity for efficient connection management.

Technologies & Tools

Backend
Durable Objects
Used to manage WebSocket connections and scale logging solutions for AI Gateway.
Protocol
Websocket
Enables persistent communication between client applications and the AI Gateway.
Service
Cloudflare API
Provides authentication and access control for the AI Gateway.

Key Actionable Insights

1
Utilize the new WebSockets API to maintain persistent connections for your AI applications, which can significantly reduce latency and improve performance.
This approach is particularly beneficial for applications that require real-time data processing and interaction with AI models, as it minimizes the overhead associated with traditional HTTP connections.
2
Implement authentication using Cloudflare API tokens to secure your WebSocket connections, ensuring that only authorized requests are processed.
This security measure is crucial for protecting sensitive data and maintaining the integrity of your AI Gateway interactions.
3
Leverage Durable Objects to scale your WebSocket connections effectively, allowing for better resource management and handling of concurrent requests.
This is especially useful in high-traffic scenarios where multiple clients may be connecting simultaneously to your AI services.

Common Pitfalls

1
Failing to include the required authentication token in WebSocket requests can lead to request failures.
This occurs because the AI Gateway enforces authentication, and without the token, requests will be rejected. Developers should ensure that their WebSocket connections are properly authenticated to avoid disruptions.

Related Concepts

Websocket Protocols
Cloudflare Developer Platform
Durable Objects
AI Gateway