tencent cloud

Serverless Cloud Function

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Related Concepts
How It Works
Strengths
Scenarios
Related Products
Purchase Guide
Billing Overview
Billing Mode
Billable Items and Billing Modes
Function Computing Power Support
Free Tier
SCF Pricing
Billing Example
Payment Overdue
Getting Started
Creating Event Function in Console
User Guide
Quota Management
Managing Functions
Web Function Management
Log Management
Concurrence Management
Trigger Management
Function URL
A Custom Domain Name
Version Management
Alias Management
Permission Management
Running Instance Management
Plugin Management
Managing Monitors and Alarms
Network Configuration
Layer Management
Execution Configuration
Extended Storage Management
DNS Caching Configuration
Resource Managed Mode Management
Near-Offline Resource Hosting Model
Workflow
Triggers
Trigger Overview
Trigger Event Message Structure Summary
API Gateway Trigger
COS Trigger
CLS Trigger
Timer Trigger
CKafka Trigger
Apache Kafka Trigger
MQTT Trigger
Trigger Configuration Description
MPS Trigger
CLB Trigger Description
TencentCloud API Trigger
Development Guide
Basic Concepts
Testing a Function
Environment Variables
Dependency Installation
Using Container Image
Error Types and Retry Policies
Dead Letter Queue
Connecting SCF to Database
Automated Deployment
Cloud Function Status Code
Common Errors and Solutions
Developer Tools
Serverless Web IDE
Calling SDK Across Functions
Third-Party Tools
Code Development
Python
Node.js
Golang
PHP
Java
Custom Runtime
Deploying Image as Function
Web Framework Development
Deploying Framework on Command Line
Quickly Deploying Egg Framework
Quickly Deploying Express Framework
Quickly Deploying Flask Framework
Quickly Deploying Koa Framework
Quickly Deploying Laravel Framework
Quickly Deploying Nest.js Framework
Quickly Deploying Next.js Framework
Quickly Deploying Nuxt.js Framework
Quickly Deploying Django Framework
Use Cases
Overview
Solutions with Tencent Cloud Services
Business Development
TRTC Practices
COS Practices
CKafka Practice
CLS
CLB Practice
MPS
CDN
CDWPG
VOD
SMS
ES
Scheduled Task
Video Processing
Success Stories
Tencent Online Education
Online Video Industry
Tencent Online Education
Best Practice of Tencent IEG Going Global
API Documentation
History
Introduction
API Category
Making API Requests
Other APIs
Namespace APIs
Layer Management APIs
Async Event Management APIs
Trigger APIs
Function APIs
Function and Layer Status Description
Data Types
Error Codes
SDK Documentation
FAQs
General
Web Function
Billing FAQs
Network FAQs
Log FAQs
SCF utility class
Event Handling FAQs
API Gateway Trigger FAQs
Related Agreement
Service Level Agreement
Contact Us
Glossary

WebSocket Protocol Support

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2024-12-02 20:07:18
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.
In addition, for the API Gateway trigger, you need to configure WebSocket (WS) or WebSocket Secure (WSS) as the frontend protocol and configure the currently specified WebSocket-enabled HTTP-triggered function as the backend. The WebSocket path provided by API Gateway can be used for client connection.

Establishing WebSocket connection

The WebSocket client uses the WebSocket link provided by the API Gateway trigger to initiate a WebSocket connection. API Gateway and SCF will pass through the connection to the service process in the runtime environment. The negotiation and communication for connection establishment are both processed on the server through 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 an operation of ending or closing the connection, and the end status code is 1000, 1010 (sent by client), or 1011 (sent by server).
The function ends normally after execution, and the execution status is "success".
200
The client or server initiates an operation of ending or closing the connection, and the end status code is not 1000, 1010, or 1011.
The function ends exceptionally, and the execution status is "failure".
439 (closed by server) or 456 (closed by client)
The connection is closed by SCF when there are no upstream or downstream messages sent over the WebSocket connection within the configured idle timeout period.
The function ends exceptionally, and the execution status is "failure".
455
The connection is closed by SCF as it is used continuously after being established and the function execution duration reaches the maximum value.
The function ends exceptionally, and the execution status is "failure".
433
For more information on WebSocket status codes for connection end, see WebSocket Status Codes.
For more information on function status codes, see Function Status Code.

Usage Limits

Use of WebSocket has the following limits:
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 request or response packet size: 256 KB. You can submit a ticket to increase the quota limit.
Maximum request size per connection: 128 KB/s. You can submit a ticket to increase the quota limit.
Maximum request QPS per connection: 10. You can submit a ticket to increase the quota limit.

Directions

Creating a function

1. Log in to the Serverless console.
2. Click Create to create a function. You can select Custom > HTTP-Triggered Function > Advanced Configuration to view the supported protocols as shown below:

3. Select WebSocket Support and configure WebSocket Idle Timeout to support the WebSocket as shown below:

4. After WebSocket Support is selected, the supported protocols of API Gateway are also automatically switched to WS&WSS, and the link provided by the created API gateway will also be a WebSocket address as shown below:

Note:
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:
Demo for Python: Use the websockets library to implement the WebSocket server.
Demo for Node.js: Use the ws library to implement the WebSocket server.

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan