WebSockets
Cloudflare supports proxied WebSocket connections without additional configuration.
WebSockets are open connections sustained between the client and the origin server. Inside a WebSockets connection, the client and the origin can pass data back and forth without having to reestablish sessions. This makes exchanging data within a WebSockets connection fast. WebSockets are often used for real-time applications such as live chat and gaming.
To enable WebSockets connections to your origin server in the dashboard:
- Log in to your Cloudflare account ↗ and go to a specific domain.
- Go to Network.
- For WebSockets, switch the toggle to On.
To enable WebSockets connections to your origin server with the API, send a PATCH
request with websockets
as the setting name in the URI path, and the value
parameter set to "on"
.
Product | Compatible | Notes |
---|---|---|
SSL | Yes | |
WAF | Yes* | The initial HTTP 101 request is subject to WAF managed rules, custom rules, rate limiting rules, and other WAF features like any other WebSockets connection. However, once a connection has been established, the WAF does not perform any further inspections. |
Workers | Yes | You can also use Durable Objects as an endpoint for WebSocket sessions, giving you full control over messages sent to and from clients. |
WebSockets are supported on all Cloudflare plans.
When Cloudflare releases new code to its global network, we may restart servers, which terminates WebSockets connections.
- Implement a keepalive ↗.
- Review and then remove or extend timeout settings on the origin and/or on the client.
Investigating issues with Websocket can be facilitated with client tools like wscat ↗. Being able to reproduce an issue on a single URL with a minimalistic tool helps narrowing down the issue.