[TOC]

With the rapid growth in digital currency trading volumes, the efficiency and stability of real-time data push services have become crucial. MEXC Exchange’s latest WebSocket push solution based on Protocol Buffers (protobuf) offers higher transmission efficiency and lower latency compared to traditional JSON format. This article will provide a detailed introduction on how to implement access and data processing for MEXC’s newly added WebSocket protobuf interface on the FMZ quantitative trading platform.
Protocol Buffers is a language-neutral, platform-neutral method for serializing structured data developed by Google. Compared to JSON format, protobuf offers the following advantages:
MEXC’s newly added WebSocket protobuf push interface primarily provides the following data streams:
https://www.mexc.com/zh-MY/api-docs/spot-v3/websocket-market-streams
Private PrivateAccountV3Api PrivateDealsV3Api PrivateOrdersV3Api
Public PublicAggreBookTickerV3Api PublicAggreDealsV3Api PublicBookTickerV3Api …
To implement the MEXC protobuf WebSocket interface on the FMZ platform, the following preparations are required:
Taking the establishment of a PublicAggreDealsV3Api channel connection as an example:
function main() {
var payload = {
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}
var conn = Dial("wss://wbs-api.mexc.com/ws|payload=" + JSON.stringify(payload))
while (true) {
var ret = conn.read()
if (ret) {
Log(ret)
}
}
}
Locate the required .proto files:
https://github.com/mexcdevelop // You can find the exchange’s .proto files here
syntax = "proto3";
option java_package = "com.mxc.push.common.protobuf";
option optimize_for = SPEED;
option java_multiple_files = true;
option java_outer_classname = "PushDataV3ApiWrapperProto";
message PublicAggreDealsV3Api {
repeated PublicAggreDealsV3ApiItem deals = 1;
string eventType = 2;
}
message PublicAggreDealsV3ApiItem {
string price = 1;
string quantity = 2;
int32 tradeType = 3;
int64 time = 4;
}
message PushDataV3ApiWrapper {
string channel = 1;
oneof body {
PublicAggreDealsV3Api publicAggreDeals = 314;
}
optional string symbol = 3;
optional string symbolId = 4;
optional int64 createTime = 5;
optional int64 sendTime = 6;
}
Since the FMZ platform is based on JavaScript, we need to use the protobuf.js library to handle binary data:
JS library loading address:
https://cdnjs.cloudflare.com/ajax/libs/protobufjs/7.5.3/protobuf.js
The data pushed by the WebSocket interface is binary data that needs to be decoded:
while (true) {
var ret = conn.read()
if (ret) {
const uint8arrayData = new Uint8Array(ret)
const message = PushDataV3ApiWrapper.decode(uint8arrayData)
data = PushDataV3ApiWrapper.toObject(message, {
longs: String,
enums: String,
bytes: String,
defaults: true,
arrays: true,
objects: true
})
Log("data:", data)
}
LogStatus(_D(), data)
}
let strPushDataV3ApiWrapper = `syntax = "proto3";
option java_package = "com.mxc.push.common.protobuf";
option optimize_for = SPEED;
option java_multiple_files = true;
option java_outer_classname = "PushDataV3ApiWrapperProto";
message PublicAggreDealsV3Api {
repeated PublicAggreDealsV3ApiItem deals = 1;
string eventType = 2;
}
message PublicAggreDealsV3ApiItem {
string price = 1;
string quantity = 2;
int32 tradeType = 3;
int64 time = 4;
}
message PushDataV3ApiWrapper {
string channel = 1;
oneof body {
PublicAggreDealsV3Api publicAggreDeals = 314;
}
optional string symbol = 3;
optional string symbolId = 4;
optional int64 createTime = 5;
optional int64 sendTime = 6;
}`
let code = HttpQuery("https://cdnjs.cloudflare.com/ajax/libs/protobufjs/7.5.3/protobuf.js")
let exports = {}
let module = { exports }
new Function("module", "exports", code)(module, exports)
let protobuf = module.exports
function main() {
const PushDataV3ApiWrapper = protobuf.parse(strPushDataV3ApiWrapper).root.lookupType("PushDataV3ApiWrapper")
var payload = {
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}
// proxy=socks5://x.x.x.x:xxxx
var conn = Dial("wss://wbs-api.mexc.com/ws|payload=" + JSON.stringify(payload))
var data = null
while (true) {
var ret = conn.read()
if (ret) {
const uint8arrayData = new Uint8Array(ret)
const message = PushDataV3ApiWrapper.decode(uint8arrayData)
data = PushDataV3ApiWrapper.toObject(message, {
longs: String,
enums: String,
bytes: String,
defaults: true,
arrays: true,
objects: true
})
Log("data:", data)
}
LogStatus(_D(), data)
}
}

As we can see, the binary data pushed by the interface has been received, and after decoding, readable data has been printed:
{
"channel":"[email protected]@100ms@BTCUSDT",
"symbol":"BTCUSDT",
"sendTime":"1754487330985",
"publicAggreDeals":{
"deals":[
{"price":"113897.97","quantity":"0.00003103","tradeType":2,"time":"1754487330933"},
{"price":"113897.97","quantity":"0.00095331","tradeType":2,"time":"1754487330934"},
{"price":"113897.97","quantity":"0.00154766","tradeType":2,"time":"1754487330935"}
],
"eventType":"[email protected]@100ms"
}
}
By implementing MEXC’s protobuf WebSocket interface on the FMZ platform, we have achieved the following advantages:
This solution is particularly suitable for:
This article is written based on MEXC Exchange’s latest WebSocket protobuf interface specifications, and the code examples have been tested and verified on the FMZ platform.