Skip to content

Commit 8e2943b

Browse files
[AIG]Websockets Docs (#20898)
* Websockets Docs * fixed hyperlink * minor edits * Update src/content/docs/ai-gateway/configuration/websockets-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/ai-gateway/configuration/websockets-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/ai-gateway/configuration/websockets-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/ai-gateway/configuration/websockets-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/ai-gateway/configuration/websockets-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/ai-gateway/configuration/websockets-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/ai-gateway/configuration/websockets-api/index.mdx Co-authored-by: Maddy <[email protected]> * fixed table --------- Co-authored-by: Maddy <[email protected]>
1 parent 1cd63f1 commit 8e2943b

File tree

3 files changed

+171
-13
lines changed

3 files changed

+171
-13
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: WebSockets API
3+
pcx_content_type: configuration
4+
sidebar:
5+
group:
6+
badge: Beta
7+
---
8+
9+
The AI Gateway WebSockets API provides a persistent connection for AI interactions, eliminating repeated handshakes and reducing latency. This API is divided into two categories:
10+
11+
- **Non-Realtime APIs** - Supports standard WebSocket communication for AI providers, including those that do not natively support WebSockets.
12+
- **Realtime APIs** - Designed for AI providers that offer low-latency, multimodal interactions over WebSockets.
13+
14+
## When to use WebSockets?
15+
16+
WebSockets are long-lived TCP connections that enable bi-directional, real-time and non realtime communication between client and server. Unlike HTTP connections, which require repeated handshakes for each request, WebSockets maintain the connection, supporting continuous data exchange with reduced overhead. WebSockets are ideal for applications needing low-latency, real-time data, such as voice assistants.
17+
18+
## Key benefits
19+
20+
- **Reduced overhead**: Avoid overhead of repeated handshakes and TLS negotiations by maintaining a single, persistent connection.
21+
- **Provider compatibility**: Works with all AI providers in AI Gateway. Even if your chosen provider does not support WebSockets, Cloudflare handles it for you, managing the requests to your preferred AI provider.
22+
23+
## **Key differences**
24+
25+
| Feature | Realtime APIs | Non-Realtime APIs |
26+
| :---------------------- | :------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------------- |
27+
| **Purpose** | Enables real-time, multimodal AI interactions for providers that offer dedicated WebSocket endpoints. | Supports WebSocket-based AI interactions with providers that do not natively support WebSockets. |
28+
| **Use Case** | Streaming responses for voice, video, and live interactions. | Text-based queries and responses, such as LLM requests. |
29+
| **AI Provider Support** | [Limited to providers offering real-time WebSocket APIs.](/ai-gateway/configuration/websockets-api/realtime-api/#supported-providers) | [All AI providers in AI Gateway.](/ai-gateway/providers) |
30+
| **Streaming Support** | Providers natively support real-time data streaming. | AI Gateway handles streaming via WebSockets. |
31+
32+
For details on implementation, refer to the next sections:
33+
34+
- [Realtime WebSockets API](/ai-gateway/configuration/websockets-api/realtime-api/)
35+
- [Non-Realtime WebSockets API](/ai-gateway/configuration/websockets-api/non-realtime-api/)

src/content/docs/ai-gateway/configuration/websockets-api.mdx renamed to src/content/docs/ai-gateway/configuration/websockets-api/non-realtime-api.mdx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
---
2-
title: WebSockets API
32
pcx_content_type: configuration
3+
title: Non-realtime WebSockets API
44
sidebar:
5-
badge:
6-
text: Beta
5+
order: 3
76
---
87

9-
The AI Gateway WebSockets API provides a single persistent connection, enabling continuous communication. By using WebSockets, you can establish a single connection for multiple AI requests, eliminating the need for repeated handshakes and TLS negotiations, which enhances performance and reduces latency. This API supports all AI providers connected to AI Gateway, including those that do not natively support WebSockets.
10-
11-
## When to use WebSockets?
12-
13-
WebSockets are long-lived TCP connections that enable bi-directional, real-time communication between client and server. Unlike HTTP connections, which require repeated handshakes for each request, WebSockets maintain the connection, supporting continuous data exchange with reduced overhead. WebSockets are ideal for applications needing low-latency, real-time data, such as voice assistants.
14-
15-
## Key benefits
16-
17-
- **Reduced Overhead**: Avoid overhead of repeated handshakes and TLS negotiations by maintaining a single, persistent connection.
18-
- **Provider Compatibility**: Works with all AI providers in AI Gateway. Even if your chosen provider does not support WebSockets, we handle it for you, managing the requests to your preferred AI provider.
8+
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.
199

2010
## Set up WebSockets API
2111

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
pcx_content_type: configuration
3+
title: Realtime WebSockets API
4+
sidebar:
5+
order: 2
6+
---
7+
8+
Some AI providers support real-time, low-latency interactions over WebSockets. AI Gateway allows seamless integration with these APIs, supporting multimodal interactions such as text, audio, and video.
9+
10+
## Supported Providers
11+
12+
- [OpenAI](https://platform.openai.com/docs/guides/realtime-websocket)
13+
- [Google AI Studio](https://ai.google.dev/gemini-api/docs/multimodal-live)
14+
- [Cartesia](https://docs.cartesia.ai/api-reference/tts/tts)
15+
- [ElevenLabs](https://elevenlabs.io/docs/conversational-ai/api-reference/conversational-ai/websocket)
16+
17+
## Authentication
18+
19+
For real-time WebSockets, authentication can be done using:
20+
21+
- Headers (for non-browser environments)
22+
- `sec-websocket-protocol` (for browsers)
23+
24+
## Examples
25+
26+
### OpenAI
27+
28+
```javascript
29+
import WebSocket from "ws";
30+
31+
const url =
32+
"wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/openai?model=gpt-4o-realtime-preview-2024-12-17";
33+
const ws = new WebSocket(url, {
34+
headers: {
35+
"cf-aig-authorization": process.env.CLOUDFLARE_API_KEY,
36+
Authorization: "Bearer " + process.env.OPENAI_API_KEY,
37+
"OpenAI-Beta": "realtime=v1",
38+
},
39+
});
40+
41+
ws.on("open", () => console.log("Connected to server."));
42+
ws.on("message", (message) => console.log(JSON.parse(message.toString())));
43+
44+
ws.send(
45+
JSON.stringify({
46+
type: "response.create",
47+
response: { modalities: ["text"], instructions: "Tell me a joke" },
48+
}),
49+
);
50+
```
51+
52+
### Google AI Studio
53+
54+
```javascript
55+
const ws = new WebSocket(
56+
"wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/google?api_key=<google_api_key>",
57+
["cf-aig-authorization.<cloudflare_token>"],
58+
);
59+
60+
ws.on("open", () => console.log("Connected to server."));
61+
ws.on("message", (message) => console.log(message.data));
62+
63+
ws.send(
64+
JSON.stringify({
65+
setup: {
66+
model: "models/gemini-2.0-flash-exp",
67+
generationConfig: { responseModalities: ["TEXT"] },
68+
},
69+
}),
70+
);
71+
```
72+
73+
### Cartesia
74+
75+
```javascript
76+
const ws = new WebSocket(
77+
"wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/cartesia?cartesia_version=2024-06-10&api_key=<cartesia_api_key>",
78+
["cf-aig-authorization.<cloudflare_token>"],
79+
);
80+
81+
ws.on("open", function open() {
82+
console.log("Connected to server.");
83+
});
84+
85+
ws.on("message", function incoming(message) {
86+
console.log(message.data);
87+
});
88+
89+
ws.send(
90+
JSON.stringify({
91+
model_id: "sonic",
92+
transcript: "Hello, world! I'm generating audio on ",
93+
voice: { mode: "id", id: "a0e99841-438c-4a64-b679-ae501e7d6091" },
94+
language: "en",
95+
context_id: "happy-monkeys-fly",
96+
output_format: {
97+
container: "raw",
98+
encoding: "pcm_s16le",
99+
sample_rate: 8000,
100+
},
101+
add_timestamps: true,
102+
continue: true,
103+
}),
104+
);
105+
```
106+
107+
### ElevenLabs
108+
109+
```javascript
110+
const ws = new WebSocket(
111+
"wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/elevenlabs?agent_id=<elevenlabs_agent_id>",
112+
[
113+
"xi-api-key.<elevenlabs_api_key>",
114+
"cf-aig-authorization.<cloudflare_token>",
115+
],
116+
);
117+
118+
ws.on("open", function open() {
119+
console.log("Connected to server.");
120+
});
121+
122+
ws.on("message", function incoming(message) {
123+
console.log(message.data);
124+
});
125+
126+
ws.send(
127+
JSON.stringify({
128+
text: "This is a sample text ",
129+
voice_settings: { stability: 0.8, similarity_boost: 0.8 },
130+
generation_config: { chunk_length_schedule: [120, 160, 250, 290] },
131+
}),
132+
);
133+
```

0 commit comments

Comments
 (0)