Skip to main content
Version: 3.x

Taro.onSocketMessage(callback)

Listens on the event of receiving server messages by WebSocket

Reference

Type

<T = any>(callback: Callback<T>) => void

Parameters

Callback

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

(result: CallbackResult<T>) => void
PropertyType
resultCallbackResult<T>

CallbackResult

PropertyTypeDescription
dataTMessages returned by the server

Sample Code

Taro.connectSocket({
url: 'test.php'
})
Taro.onSocketMessage(function (res) {
console.log('receive msg:' + res.data)
})

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
Taro.onSocketMessage✔️✔️✔️