Skip to main content
Version: Next

SocketTask

The WebSocket task can be created and returned via the Taro.connectSocket() API.

Reference

PropertyTypeDescription
socketTaskIdnumberThe current connection ID of the websocket.
readyStatenumberThe current connection status of the websocket.
errMsgstringThe result of the call to the websocket interface.
CONNECTINGnumberwebsocket status value: Connecting.
OPENnumberwebsocket status value: Connected.
CLOSINGnumberwebsocket status value: Closing.
CLOSEDnumberwebsocket status value: Closed.
wsWebSocketBrowser websocket instances. (Only H5)

Methods

close

Disables the WebSocket connection

Reference

(option: CloseOption) => void
PropertyType
optionCloseOption

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
SocketTask.close✔️✔️✔️✔️✔️

onClose

Listens on the event of disabling the WebSocket connection.

Reference

(callback: OnCloseCallback) => void
PropertyTypeDescription
callbackOnCloseCallbackThe callback function for the event of disabling the WebSocket connection.

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
SocketTask.onClose✔️✔️✔️✔️✔️

onError

Listens on the WebSocket error event.

Reference

(callback: OnErrorCallback) => void
PropertyTypeDescription
callbackOnErrorCallbackThe callback function for the WebSocket error event.

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
SocketTask.onError✔️✔️✔️✔️✔️

onMessage

Listens on the event of receiving server messages by WebSocket

Reference

<T = any>(callback: OnMessageCallback<T>) => void
PropertyTypeDescription
callbackTThe callback function for the event of receiving server messages by WebSocket.

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
SocketTask.onMessage✔️✔️✔️✔️✔️

onOpen

Listens on the event of enabling the WebSocket connection.

Reference

(callback: OnOpenCallback) => void
PropertyTypeDescription
callbackOnOpenCallbackThe callback function for the event of enabling the WebSocket connection.

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
SocketTask.onOpen✔️✔️✔️✔️✔️

send

Sends data over a WebSocket connection

Reference

(option: SendOption) => void
PropertyType
optionSendOption

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
SocketTask.send✔️✔️✔️✔️✔️

Parameters

CloseOption

PropertyTypeRequiredDescription
codenumberNoA numeric value indicates the status code explaining why the connection has been disabled.
reasonstringNoA readable string explaining why the connection has been disabled.
complete(res: CallbackResult) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: CallbackResult) => voidNoThe callback function for a successful API call

OnCloseCallback

The callback function for the event of disabling the WebSocket connection.

(result: OnCloseCallbackResult) => void
PropertyType
resultOnCloseCallbackResult

OnCloseCallbackResult

PropertyTypeDescription
codenumberA numeric value indicates the status code explaining why the connection has been disabled.
reasonstringA readable string explaining why the connection has been disabled.

OnErrorCallback

The callback function for the WebSocket error event.

(result: OnErrorCallbackResult) => void
PropertyType
resultOnErrorCallbackResult

OnErrorCallbackResult

PropertyTypeDescription
errMsgstringError message

OnMessageCallback

The callback function for the event of receiving server messages by WebSocket.

(result: OnMessageCallbackResult<T>) => void
PropertyType
resultOnMessageCallbackResult<T>

OnMessageCallbackResult

PropertyTypeDescription
dataTMessages returned by the server

OnOpenCallback

The callback function for the event of enabling the WebSocket connection.

(result: OnOpenCallbackResult) => void
PropertyType
resultOnOpenCallbackResult

OnOpenCallbackResult

PropertyTypeDescription
headerRecord<string, any>Connected HTTP response header

SendOption

PropertyTypeRequiredDescription
datastring | ArrayBufferYesThe data to be sent
complete(res: CallbackResult) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: CallbackResult) => voidNoThe callback function for a successful API call

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
SocketTask.close✔️✔️✔️✔️✔️
SocketTask.onClose✔️✔️✔️✔️✔️
SocketTask.onError✔️✔️✔️✔️✔️
SocketTask.onMessage✔️✔️✔️✔️✔️
SocketTask.onOpen✔️✔️✔️✔️✔️
SocketTask.send✔️✔️✔️✔️✔️