TikTok for Developers

Developer Docs

Last updated October 1, 2026

Websocket

The WebSocket APIs enable real-time, bidirectional communication between your mini game and a developer server through a persistent connection. Use TTMinis.game.connectSocket to establish a connection, then use the returned SocketTask object to send messages, register event listeners, and manage the connection lifecycle.

Note: Websocket JavaScript APIs are supported starting from SDK version 0.4.0. Lower versions must be adjusted for compatibility.

TTMinis.game.connectSocket(opts)

Creates a WebSocket connection.

Note: Supported from base library version 0.4.0. Lower versions require compatibility handling.

Parameters

Field

Type

Description

Required

url

string

The developer server WSS endpoint URL

Yes

header

object

The HTTP request headers. The Referer header cannot be set.

No

protocols

list<string>

The sub-protocol array

No

timeout

int32

The timeout duration in milliseconds

No

success

function

The callback function called when the API call succeeds

No

fail

function

The callback function called when the API call fails

No

complete

function

The callback function called when the API call ends, regardless of success or failure

No

Returns

A SocketTask object that can be used to monitor and control the WebSocket connection.

Example

TTMinis.game.connectSocket({
  url: 'wss://EXAMPLE_WSS_ENDPOINT',
  header: {
    'content-type': 'application/json'
  }
})

TTMinis.game.SocketTask.close(opts)

Closes the WebSocket connection.

Parameters

Field

Type

Description

Required

code

int32

A numeric status code indicating the reason for closing the connection. Default is 1000 (normal closure).

No

reason

string

A human-readable string explaining the reason for closing the connection. Must be no longer than 123 bytes of UTF-8 text (not characters).

No

success

function

The callback function called when the API call succeeds

No

fail

function

The callback function called when the API call fails

No

complete

function

The callback function called when the API call ends, regardless of success or failure

No

Example

socketTask.close({
  code: 1000,
  reason: 'Normal closure'
})

TTMinis.game.SocketTask.onOpen(listener)

Registers a listener for the WebSocket connection open event.

Parameters

Field

Type

Description

Required

listener

function

The listener function for the WebSocket connection open event

Yes

Listener callback parameters

Field

Type

Description

header

object

The HTTP response headers from the successful connection

profile

object

Debug information from the network request process

Example

socketTask.onOpen((res) => {
  console.log('WebSocket connected', res.header)
})

TTMinis.game.SocketTask.onMessage(listener)

Registers a listener for the WebSocket message received event.

Parameters

Field

Type

Description

Required

listener

function

The listener function for the WebSocket message received event

Yes

Listener callback parameters

Field

Type

Description

data

string | ArrayBuffer

The message returned by the server

Example

socketTask.onMessage((res) => {
  console.log('Message received:', res.data)
})

TTMinis.game.SocketTask.onClose(listener)

Registers a listener for the WebSocket connection close event.

Parameters

Field

Type

Description

Required

listener

function

The listener function for the WebSocket connection close event

Yes

Listener callback parameters

Field

Type

Description

code

int32

A numeric status code indicating the reason for closing the connection

reason

string

A human-readable string explaining the reason for closing the connection

Example

socketTask.onClose((res) => {
  console.log('WebSocket closed', res.code, res.reason)
})

TTMinis.game.SocketTask.onError(listener)

Registers a listener for the WebSocket error event.

Parameters

Field

Type

Description

Required

listener

function

The listener function for the WebSocket error event

Yes

Listener callback parameters

Field

Type

Description

errCode

int32

The error code

errMsg

string

The error message

Example

socketTask.onError((res) => {
  console.error('WebSocket error', res.errCode, res.errMsg)
})

TTMinis.game.SocketTask.send(opts)

Sends data through the WebSocket connection.

Parameters

Field

Type

Description

Required

data

string | ArrayBuffer

The content to send

Yes

success

function

The callback function called when the API call succeeds

No

fail

function

The callback function called when the API call fails

No

complete

function

The callback function called when the API call ends, regardless of success or failure

No

Example

socketTask.send({
  data: 'Hello, server'
})
TikTok for Developers