Skip to main content

WebSocket Chat

Establish Connection

Connection URL: wss://wbs.p2p.mexc.com/ws

Required Permission: P2P Deal Order, P2P Deal Ad

Monitor connection status in real time. Reconnect when the connection is lost.

Connection Parameters

NameMandatoryDescription
listenKeyyesGenerated via Generate listenKey API
conversationIdyesObtained via Get Chat Conversation Info API

Connection Example

wss://wbs.p2p.mexc.com/ws?listenKey=pqia91ma19a5s61cv6a81va65sd099v8a65a1&conversationId=123

Send Message

Request

{
"method": "SEND_MESSAGE",
"params": "{\"content\":\"This is a design screenshot\",\"conversationId\":50012,\"type\":2,\"imageUrl\":\"https://cdn.example.com/images/design_v3.png\",\"imageThumbUrl\":\"https://cdn.example.com/images/design_v3_thumb.jpg\",\"videoUrl\":\"\",\"fileUrl\":\"\"}"
}

Message Body Fields (JSON in params)

NameTypeDescription
contentstringMessage content
conversationIdintegerConversation ID
typeintegerMessage type: 1=text, 2=image, 3=video, 4=file
imageUrlstringImage URL (required when type=2)
imageThumbUrlstringImage thumbnail URL
videoUrlstringVideo URL (required when type=3)
fileUrlstringFile URL (required when type=4)

Response

{
"success": true,
"method": "SEND_MESSAGE",
"msg": "success"
}

Receive Message

Server pushes messages to the client:

{
"success": true,
"msg": "success",
"method": "RECEIVE_MESSAGE",
"data": "{\"id\":1,\"content\":\"hello\",\"conversationId\":50012,\"type\":1,\"createTime\":\"2025-01-26 12:00:00\",\"self\":false,\"fromUserId\":\"abc123\",\"fromNickName\":\"Counterparty\"}"
}

Message Body Fields (JSON in data)

NameTypeDescription
idintegerMessage ID
contentstringMessage content
conversationIdintegerConversation ID
typeintegerMessage content type: 1=text, 2=image, 3=video, 4=file
imageUrlstringImage URL
imageThumbUrlstringImage thumbnail URL
videoUrlstringVideo URL
fileUrlstringFile URL
createTimestringSend time
selfbooleanWhether the message was sent by self
fromUserIdstringSender ID
fromNickNamestringSender nickname

Heartbeat

Send a heartbeat every 5 seconds. The server disconnects if no heartbeat is received for more than 60 seconds.

Request

{"method":"PING"}

Response

{
"success": true,
"method": "PING",
"msg": "success",
"data": "PONG"
}