Using the WebSockets API
WebSockets allow you to communicate in real time with your Cloudflare Workers serverless functions. In this guide, you will learn the basics of WebSockets on Cloudflare Workers, both from the perspective of writing WebSocket servers in your Workers functions, as well as connecting to and working with those WebSocket servers as a client.
WebSockets are open connections sustained between the client and the origin server. Inside a WebSocket connection, the client and the origin can pass data back and forth without having to reestablish sessions. This makes exchanging data within a WebSocket connection fast. WebSockets are often used for real-time applications such as live chat and gaming.
WebSocket servers in Cloudflare Workers allow you to receive messages from a client in real time. This guide will show you how to set up a WebSocket server in Workers.
A client can make a WebSocket request in the browser by instantiating a new instance of WebSocket
, passing in the URL for your Workers function:
When an incoming WebSocket request reaches the Workers function, it will contain an Upgrade
header, set to the string value websocket
. Check for this header before continuing to instantiate a WebSocket:
After you have appropriately checked for the Upgrade
header, you can create a new instance of WebSocketPair
, which contains server and client WebSockets. One of these WebSockets should be handled by the Workers function and the other should be returned as part of a Response
with the 101
status code, indicating the request is switching protocols:
The WebSocketPair
constructor returns an Object, with the 0
and 1
keys each holding a WebSocket
instance as its value. It is common to grab the two WebSockets from this pair using Object.values
and ES6 destructuring, as seen in the below example.
In order to begin communicating with the client
WebSocket in your Worker, call accept
on the server
WebSocket. This will tell the Workers runtime that it should listen for WebSocket data and keep the connection open with your client
WebSocket:
WebSockets emit a number of Events that can be connected to using addEventListener
. The below example hooks into the message
event and emits a console.log
with the data from it:
Writing WebSocket clients that communicate with your Workers function is a two-step process: first, create the WebSocket instance, and then attach event listeners to it:
WebSocket clients can send messages back to the server using the send
function:
When the WebSocket interaction is complete, the client can close the connection using close
:
For an example of this in practice, refer to the websocket-template
to get started with WebSockets.
Cloudflare Workers supports the new WebSocket(url)
constructor. A Worker can establish a WebSocket connection to a remote server in the same manner as the client implementation described above.
Additionally, Cloudflare supports establishing WebSocket connections by making a fetch request to a URL with the Upgrade
header set.
Cloudflare Workers supports WebSocket compression. Refer to WebSocket Compression for more information.