Currently, an HTTP-triggered function allows you to connect the client to the server where it runs over the native WebSocket protocol.
How It Works
Starting service
You can use a bootstrap file to start the WebSocket server in the runtime environment of the HTTP-triggered function configured with support for the WebSocket protocol and listen on the specified port (9000) to wait for client connections.
Simultaneously, the API Gateway trigger must be set to support the "WS or WSS" frontend protocol, with the backend being the currently specified HTTP-triggered function that supports WebSocket. The ws path provided by the API Gateway can be used for client connections.
Establishing WebSocket connection
The WebSocket client initiates a WebSocket connection using the WS connection provided by the API Gateway trigger. The API Gateway and the cloud function platform will transparently pass the connection to the service process in the runtime environment. The negotiation and communication process of establishing the connection are all handled by the server-side code.
After the connection is established, the client and server normally communicate over the WebSocket protocol.
WebSocket connection lifecycle
If an HTTP-triggered function supports WebSocket, the lifecycle of a WebSocket connection is the same as an invocation request of the function, the WebSocket connection establishment process equals request initiation, and disconnection equals request end.
Plus, function instances and connections are in one-to-one correspondence; that is, one instance only processes one WebSocket connection at a time. When more client connection requests are initiated, the same number of instances will be started for processing.
When a WebSocket connection request is initiated, a function instance will be started and receive the connection request.
After the WebSocket connection is established, the instance will run continuously and receive and process the upstream data from the client based on the actual business conditions. Or, the server actively pushes the downstream data.
After the WebSocket connection is closed, the instance will stop running.
Disconnection
A WebSocket connection will be closed in the following cases, and the current request execution will also end, as the request lifecycle is the same as the connection lifecycle:
Disconnection Conditions
Function Status
Function Status Code
The client or server initiates a connection termination or closes the connection. The termination status codes are 1000 and 1010 (sent by the client), and 1011 (sent by the server).
The function executes normally and the operation status is successful.
200
The client or server initiates a connection termination or closes the connection, with termination status codes other than 1000, 1010, and 1011.
The function terminates abnormally, resulting in a failed operation status.
439 (Server-side closure)
456 (Client-side closure)
In the absence of any message being sent upstream or downstream on the WS connection, reaching the configured idle timeout, the connection is severed by the function platform.
The function terminates abnormally, resulting in a failed operation status.
455
Upon continuous usage after the connection is established, once the function runtime reaches its maximum duration, the connection is severed by the function platform.
The function terminates unexpectedly, resulting in a failure status.
433
For more information on WebSocket status codes for connection end, see WebSocket Status Codes.
Idle timeout period: 10–7200 seconds. The execution timeout period of a function must be greater than or equal to the idle timeout period.
Maximum size of a single request or response packet: 256KB. To increase the quota limit, please contact us.
Single connection request size limit: 128KB/s. To increase the quota limit, please contact us.
Single connection request QPS limit: 10. To increase the quota limit, please contact us.
Procedure
Creating a Function
1. Log in to the Serverless console and click on Function Service in the left navigation bar.
2. Select the region and namespace where to create a function at the top of the page and click Create to enter the function creation process.
3. Choose to create a new function from scratch, and select HTTP-triggered Function as the function type.
4. In Advanced Settings, view the supported protocols. By checking WebSocket support and configuring the WebSocket Idle Timeout, you can complete the WebSocket protocol support. As shown below:
5. After enabling WebSocket support, in Trigger configurations, the protocol support of the API Gateway will also automatically switch to WS&WSS support. The link address provided by the created API Gateway will also be a WebSocket address. As shown below:
Description
After creation, the support for WebSocket protocol cannot be canceled, but the idle timeout period can be changed as needed.
Sample code
You can use the following demos to create a function and try out WebSocket: