Skip to main content

Protocol Buffers Integration

The current websocket push uses the protobuf format. The specific integration process is as follows:

1.PB File Definition
The PB definition files can be obtained via the provided link:https://github.com/mexcdevelop/websocket-proto

2.Generate Deserialization Code
Use the tool available at https://github.com/protocolbuffers/protobuf to compile the .proto files and generate deserialization code.

Java

protoc *.proto --java_out=python custom_path

Python

protoc *.proto --python_out=python custom_path

Others

Multiple languages are supported, including C++, C#, Go, Ruby, PHP, JS, etc. For details, see <a href="https://github.com/protocolbuffers/protobuf" title="https://github.com/protocolbuffers/protobuf" aria-label="https://github.com/protocolbuffers/protobuf" rel="nofollow">https://github.com/protocolbuffers/protobuf</a>.

3.Data Deserialization
Use the code generated in the previous step to deserialize the data.

Java
Include the protobuf-java dependency:

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>{protobuf.version}</version> <!-- Specify the version as per your project requirements -->
</dependency>
//Parsing example:

// Assemble the object
PushDataV3ApiWrapper pushDataV3ApiWrapper = PushDataV3ApiWrapper.newBuilder()
.setChannel("spot@public.aggre.depth.v3.api.pb@10ms")
.setSymbol("BTCUSDT")
.setSendTime(System.currentTimeMillis())
.build();

// Serialize to a byte array
byte[] serializedData = pushDataV3ApiWrapper.toByteArray();

// Deserialize into a PushDataV3ApiWrapper object
PushDataV3ApiWrapper resultV3 = PushDataV3ApiWrapper.parseFrom(serializedData);

Python

#Parsing example:

import PushDataV3ApiWrapper_pb2

# Assemble the object
pushData = PushDataV3ApiWrapper_pb2.PushDataV3ApiWrapper()
pushData.channel = 'spot@public.aggre.depth.v3.api.pb@10ms'
pushData.symbol = 'BTCUSDT'

# Serialize to a string
serializedData = pushData.SerializeToString()

# Deserialize into a PushDataV3ApiWrapper object
result = PushDataV3ApiWrapper_pb2.PushDataV3ApiWrapper()
result.ParseFromString(serializedData)
print(result)

Subscribe to a Data Stream

Subscription Channel Response

{
"id": 0,
"code": 0,
"msg": "spot@public.aggre.deals.v3.api.pb@100ms@BTCUSDT"
}
  • Request
{
"method": "SUBSCRIPTION",
"params": ["spot@public.aggre.deals.v3.api.pb@100ms@BTCUSDT"]
}

Unsubscribe from a Data Stream

Unsubscription Response

{
"id": 0,
"code": 0,
"msg": "spot@public.aggre.deals.v3.api.pb@100ms@BTCUSDT"
}
  • Request
{
"method": "UNSUBSCRIPTION",
"params": ["spot@public.aggre.deals.v3.api.pb@100ms@BTCUSDT"]
}

PING/PONG Mechanism

PING/PONG Response

{
"id": 0,
"code": 0,
"msg": "PONG"
}
  • Request
{"method": "PING"}