WebSockets API

Last updated Jun 12, 2026 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:

  • Realtime APIs - Designed for AI providers that offer low-latency, multimodal interactions over WebSockets.
  • Non-Realtime APIs - Supports standard WebSocket communication for AI providers, including those that do not natively support WebSockets.

When to use WebSockets

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.

Key benefits

  • Reduced overhead: Avoid overhead of repeated handshakes and TLS negotiations by maintaining a single, persistent connection.
  • 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.

Key differences

Feature Realtime APIs Non-Realtime APIs
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.
Use Case Streaming responses for voice, video, and live interactions. Text-based queries and responses, such as LLM requests.
AI Provider Support Limited to providers offering real-time WebSocket APIs. All AI providers in AI Gateway.
Streaming Support Providers natively support real-time data streaming. AI Gateway handles streaming via WebSockets.

For details on implementation, refer to the next sections:

Was this helpful?

YesNo

On this page

WebSockets API - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ai-gateway/usage/websockets-api/#page","headline":"WebSockets API","description":"Use persistent WebSocket connections through AI Gateway for real-time and non-realtime AI interactions.","url":"https://developers.cloudflare.com/ai-gateway/usage/websockets-api/","inLanguage":"en","image":"https://developers.cloudflare.com/ai-gateway/usage/websockets-api/og.png?v=0a3734246385e936","dateModified":"2026-06-12","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Establish persistent WebSocket connections for AI requests through AI Gateway without real-time streaming. title: Non-realtime WebSockets API image: https://developers.cloudflare.com/ai-gateway/usage/websockets-api/non-realtime-api/og.png?v=7cf74d3088842ed8


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/ai-gateway/llms.txt
Use this file to discover all available pages before exploring further.