tencent cloud

Tencent Cloud Super App as a Service

Release Notes and Announcements
Announcement: Tencent Cloud Mini Program Platform Renamed to Tencent Cloud Super App as a Service on January 2, 2025
Console Updates
Android SDK Updates
iOS SDK Updates
Flutter SDK Updates
IDE Updates
Base Library Updates
Product Introduction
Overview
Strengths
Use Cases
Purchase Guide
Billing Overview
Pay-As-You-Go Billing
Renewal Guide
Service Suspension Instructions
Getting Started
Plan Management
Overview
Console Account Management
Storage Configuration
Acceleration Configuration
Branding Configurations
Platform Features
Console Login
Users and Permission System
Mini Program Management
Mini Game Management
Superapp Management
Commercialization
Platform Management
User Management
Team Management
Operations Management
Security Center
Code Integration Guide
Getting Demo and SDK
Android
iOS
Flutter
Superapp Server
GUID Generation Rules
Mini Program Development Guide
Mini Program Introduction and Development Environment
Mini Program Code Composition
Guide
Framework
Components
API
Server Backend
JS SDK
Base Library
IDE Operation Instructions
Mini Game Development Guide
Guide
API
Server Backend
Practice Tutorial
Mini Program Login Practical Tutorial
Mini Program Subscription Message Practical Tutorial
Payment Practical Tutorial
Ad Integration Practical Tutorial
Mini Game Subscription Message Practical Tutorial
API Documentation
History
Introduction
API Category
Making API Requests
Operation Management APIs
User Management APIs
Team Management APIs
Sensitive API-Related APIs
Role Management APIs
Platform Management APIs
Other Console APIs
Mini Program or Mini Game APIs
Management-Sensitive APIs
Global Domain Management APIs
Superapp APIs
Data Types
Agreements
Service Level Agreement
Data Processing and Security Agreement
SDK Privacy Policy Module
SDK Data Processing and Security Agreement Module

Websocket

PDF
Focus Mode
Font Size
Last updated: 2025-03-20 18:01:56
Note:
It is advisable to utilize the SocketTask method to manage WebSocket connections, as it provides greater control over the lifecycle of each link. In scenarios where multiple WebSocket connections exist, the use of methods with a wx prefix may result in situations that deviate from expectations.

sendSocketMessage

The usage of this API is as follows: wx.sendSocketMessage(Object object)
Function Description: This feature allows data transmission via WebSocket connection. It requires an initial wx.connectSocket, and can only transmit data after the wx.onSocketOpen callback.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
data
string/ArrayBuffer
-
Supported
Content to be dispatched
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
Sample Code:
let socketOpen = false
let socketMsgQueue = []
wx.connectSocket({
url: 'test.php'
})

wx.onSocketOpen(function(res) {
socketOpen = true
for (let i = 0; i < socketMsgQueue.length; i++){
sendSocketMessage(socketMsgQueue[i])
}
socketMsgQueue = []
})

function sendSocketMessage(msg) {
if (socketOpen) {
wx.sendSocketMessage({
data:msg
})
} else {
socketMsgQueue.push(msg)
}
}

onSocketOpen

The usage method for this API is wx.onSocketOpen(function listener)
Function Description: Monitors the WebSocket connection opening event.
Parameters and Description: function listener, the monitoring function for the WebSocket connection opening event.
Attributes
Types
Note
header
object
HTTP response Header upon successful connection

onSocketMessage

The usage method for this API is wx.onSocketMessage(function listener)
Function Description: Monitors the event of WebSocket receiving a message from the server.
Parameters and Description: function listener, the monitoring function for the event of WebSocket receiving a message from the server.
Attributes
Types
Note
data
string/ArrayBuffer
Message returned by the server

onSocketError

The usage method for this API is wx.onSocketError(function listener)
Function Description: Monitors the WebSocket error event.
Parameters and Description: function listener, the monitoring function for the WebSocket error event.
Attributes
Types
Note
errMsg
string
Error Message

onSocketClose

The usage method for this API is wx.onSocketClose(function listener)
Function Description: Monitors the WebSocket connection closure event.
Parameters and Description: function listener, the monitoring function for the WebSocket connection closure event.
Attributes
Types
Note
code
number
A numerical value representing the status number of the closed connection, indicating the reason for the connection closure.
reason
string
A readable string indicating the reason for the connection closure.

connectSocket

The usage method for this API is SocketTask wx.connectSocket(Object object)
Function Description: Establishes a WebSocket connection. Please read the relevant instructions.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
url
string
-
Supported
Developer server's wss interface address
header
Object
-
Not required
HTTP Header, the Referer cannot be set in the Header.
protocols
Array.<string>
-
Not required
Array of sub-protocols.
tcpNoDelay
boolean
false
Not required
TCP_NODELAY setting during the establishment of a TCP connection.
timeout
number
-
Not required
Timeout duration, measured in milliseconds.
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
Return Value: SocketTask.
Concurrency Count
In versions 1.5.0 and above, a maximum of five WebSocket connections can coexist simultaneously.
In versions below 1.5.0, a mini program can only maintain a single WebSocket connection at a time. If a WebSocket connection already exists, it will be automatically closed and a new WebSocket connection will be established.
Example Code
wx.connectSocket({
url: 'wss://example.qq.com',
header:{
'content-type': 'application/json'
},
protocols: ['protocol1']
})

closeSocket

The usage of this API is wx.closeSocket(Object object).
Function Description: This function closes the WebSocket connection. The method of use is as follows.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
code
number
1000 (indicating a normal closure of the connection)
Not required
A numerical value representing the status number of the closed connection, indicating the reason for the connection closure.
reason
string
-
Not required
A readable string indicating the reason for the connection closure. This string must be a UTF-8 text that does not exceed 123 bytes in length (not characters).
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
Sample Code:
wx.connectSocket({
url: 'test.php'
})

//Please note that there may be timing issues here,
//If wx.connectSocket has not yet called back wx.onSocketOpen, and wx.closeSocket is called first, then the purpose of closing the WebSocket cannot be achieved.
//It is imperative to call wx.closeSocket during the WebSocket's open period to effectuate closure.
wx.onSocketOpen(function() {
wx.closeSocket()
})

wx.onSocketClose(function(res) {
console.log('WebSocket has been closed!')
})

SocketTask

.close

The method is utilized in the form of SocketTask.close(Object object).
Function Description: This function serves to close the WebSocket connection.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
code
number
1000 (indicating a normal closure of the connection)
Not required
A numerical value representing the status number of the closed connection, indicating the reason for the connection closure.
reason
string
-
Not required
A readable string indicating the reason for the connection closure. This string must be a UTF-8 text that does not exceed 123 bytes in length (not characters).
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)

.onClose

The method is employed as SocketTask.onClose(function listener).
Function Description: Monitors the WebSocket connection closure event.
Parameters and Description: function listener, the monitoring function for the WebSocket connection closure event.
Attributes
Types
Note
code
number
A numerical value representing the status number of the closed connection, indicating the reason for the connection closure.
reason
string
A readable string indicating the reason for the connection closure.

.onError

The method is invoked as SocketTask.onError(function listener).
Function Description: Monitors the WebSocket error event.
Parameters and Description: function listener, the monitoring function for the WebSocket error event.
Attributes
Types
Note
errMsg
string
Error Message

.onMessage

The method is implemented as SocketTask.onMessage(function listener).
Function Description: Monitors the event of WebSocket receiving a message from the server.
Parameters and Description: function listener, the monitoring function for the event of WebSocket receiving a message from the server.
Attributes
Types
Note
data
string/ArrayBuffer
Message returned by the server

.onOpen

The method is executed as SocketTask.onOpen(function listener).
Function Description: Monitors the WebSocket connection opening event.
Parameters and Description: function listener, the monitoring function for the WebSocket connection opening event.
Attributes
Types
Note
header
Object
HTTP response Header upon successful connection

.send

The usage of this method is SocketTask.send(Object object).
Function Description: Data is transmitted via a WebSocket connection.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
data
string/ArrayBuffer
-
Supported
Content to be dispatched
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback