Skip to main content

Enabling WebSockets

To use WebSockets in your scripts, you’ll first need to enable it for your Pull Zone in the Dashboard.
  1. Navigate to your Pull Zone -> General -> WebSockets
  2. Toggle the “WebSockets” switch
  3. Call request.upgradeWebSocket() in your script to upgrade incoming connections

Quickstart

This example creates a WebSocket server that echoes messages back to the client.

upgradeWebSocket

Upgrades an incoming HTTP request to a WebSocket connection.

Parameters

string
The WebSocket subprotocol to use for the connection.
number
default:"30"
The number of seconds to wait for a pong response before closing the connection. If the client does not respond within this timeout, the connection is deemed unhealthy and closed, emitting the close and error events. If no data is transmitted from the client for 2 minutes, the connection will be closed regardless of this configuration.

Returns

Response
required
The response to send back to the client to establish the upgrade.
WebSocket
required
The WebSocket interface to communicate with the client.

Methods

close

Closes the WebSocket connection, optionally providing a close code and reason.

Parameters

number
A standardized WebSocket close code. If unset, defaults to 1000 for normal closure or 1001-1015 for error conditions.
string
A human-readable close reason explaining why the connection was closed.

send

Transmits data to the connected client.

Parameters

string | ArrayBufferLike | Blob | ArrayBufferView
required
The data to transmit. Can be a string, ArrayBufferLike, Blob, or ArrayBufferView.

addEventListener

Registers an event listener for WebSocket events.

Parameters

string
required
The event type to listen for. One of: open, message, close, or error.
function
required
The callback function invoked when the event is dispatched.
boolean | AddEventListenerOptions
Optional configuration for the event listener.

Events

open

Fired when the WebSocket connection is successfully established.
Event
A standard Event object with no additional properties.

message

Fired when a message is received from the client.
object

close

Fired when the WebSocket connection is closed.
object

error

Fired when an error occurs on the WebSocket connection.
Event
A standard Event object with no additional properties.

References

Last modified on June 25, 2026