Enabling WebSockets
To use WebSockets in your scripts, you’ll first need to enable it for your Pull Zone in the Dashboard.- Navigate to your Pull Zone -> General -> WebSockets
- Toggle the “WebSockets” switch
- 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
The WebSocket subprotocol to use for the connection.
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
The response to send back to the client to establish the upgrade.
The WebSocket interface to communicate with the client.
Methods
close
Closes the WebSocket connection, optionally providing a close code and reason.Parameters
A standardized WebSocket close
code. If unset,
defaults to
1000 for normal closure or 1001-1015 for error conditions.A human-readable close
reason explaining
why the connection was closed.
send
Transmits data to the connected client.Parameters
The data to transmit. Can be a string, ArrayBufferLike, Blob, or
ArrayBufferView.
addEventListener
Registers an event listener for WebSocket events.Parameters
The event type to listen for. One of:
open, message, close, or error.The callback function invoked when the event is dispatched.
Optional configuration for the event listener.
Events
open
Fired when the WebSocket connection is successfully established.A standard Event object with no additional properties.
message
Fired when a message is received from the client.close
Fired when the WebSocket connection is closed.error
Fired when an error occurs on the WebSocket connection.A standard Event object with no additional properties.