Protobuf Efficient Transmission

Source: Internet
Author: User
Tags unique id
protobuf Efficient Transmission

The use of protocol buffers can be a good solution to the lack of JSON data transmission, it is a data description language developed by Google, similar to XML can be structured data serialization, can be used in data storage, communication protocols and so on. It is not dependent on language and platform and is extremely extensible. At this stage, the official support of C + +, JAVA, Python and other three programming languages, but can find a large number of almost all languages of the third-party expansion package.
The Protobuf file uses a unique ID (number) in place of the complex key in JSON, so that the data sender and the data receiver can greatly improve the efficiency of the transmission as long as they use the same set of template files for parsing.
Protobuf.js is a bytebuffer.js-based protocol buffers pure JavaScript implementation. The main function is to parse the. proto file, build the message class, and simply encode, decode. define a protocol buffer message

Define a Addressbook.proto package
tutorial;

Message person {
 Required String name = 1;
 Required Int32 id = 2;
 Optional String email = 3;

 Enum Phonetype {
 MOBILE = 0;
 HOME = 1;
 Work = 2;
 }

 Message phonenumber{
 Required String number = 1;
 Optional Phonetype type = 2[default = HOME];
 }

 Repeated PhoneNumber phone = 4;
}

Message AddressBook {
 repeated person person = 1;
}

Detail explanation
1, package declaration in order to prevent different projects between the naming conflicts, here the tutorial equivalent to namespace.
2. Message is a collection of several types of fields that can be used with bool, Int32, float, double, and string types. You can embed a message collection, similar to a struct.
3, "1", "2" Mark identifies the unique tag of the Type field in the binary encoding, indicating the layout position of the different fields in the serialized binary data.
Tag number 1-15 is less than one byte relative to higher numbers, so you can use 1-15 tag as commonly used repeated elements,16 or higher tag for less-commonly Use is left to optional elements.
4, each field must use the following identifiers

* Required: The field value must be provided, otherwise the message will be considered uninitialized.
* Optional: Field value Optional
* Repeated: field may be repeated any number of times (including 0). The repeated field can be viewed as a dynamic size array.

5. Enum is a keyword defined by an enumeration type, and 0 and 1 represent the actual integer value corresponding to the enumeration value, and as with C + +, you can specify an arbitrary integer value for the enumeration value without always starting from the 0 definition.
6. You can define multiple messages in the same. proto file, which makes it easy to implement the definition of a nested message. Protocol buffer provides another keyword import so that we can define many generic messages in the same. proto file, while other message definition files can be included in the message defined in the file by import, such as:
Basic rules for the import "Myproject/commonmessages.proto" qualifier (required/optional/repeated)

1. You must leave at least one field of type required in each message.
2. Each message can contain 0 or more fields of the optional type.
3. Repeated represents a field that can contain 0 or more data.
4, if you intend to add a new field in the original message protocol, but also to ensure that the old version of the program can read or write normally, the newly added field must be optional or repeated. The reason is very simple, the old version of the program cannot read or write the new required qualifier field. How to use

The protobuf.js can be used to transmit the data of the front and back end with Protobuffer. By introducing NPM's PROTOBUF module, the backend converts the JSON data into a protocol binary format, which is then uploaded to the front end, and the front end receives the data via the XMLHttpRequest (an Ajax method with JQ that can cause cross-domain errors). Then introduce the PROTOBUF in the Dist folder under the Protobuf.js to parse. Test Results

JSON Json_gzip Protobuf Protobuf_gzip
18.7M 1.1M 6.1M 880k
1959ms 1551ms 1481ms 860ms

4 types of tests were used, Json,gzip compressed json,protobuf,gzip after compression of protobuf.
The first behavior file size, the second behavior of the transfer + parsing time spent. Application Scenarios

1. When you need to transfer large amounts of data, such as sending large data to the foreground of the vertex index data, the traditional JSON format transfer speed is slow, to PROTOBUF format can greatly improve efficiency.

Reference: http://www.maxzhang.com/2015/09/ProtoBuf-js%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.