JavaScript implementation of Protobuf.js–protocol buffers

Source: Internet
Author: User
Tags unique id

Source Link: JavaScript implementation of Protobuf.js–protocol buffers

Recommended front end URL:http://keenwon.com/

The most common data format in JavaScript is Json,xml, but these two formats have significant deficiencies in data transmission. and protocol buffers can be a good solution to this problem, the following reference to the encyclopedia of the definition of protocol buffers:

Protocol buffers is a data description language developed by Google, similar to the ability of XML to serialize structured data for data storage, communication protocols, and more. 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 same as the data storage format, compared to JSON, Protocol buffers advantages are mainly reflected in the performance and volume, performance needs actual testing, for the moment, the data volume advantages are more obvious, for example, a JSON file:

  1. [
  2. { "Name" : "Zhangsan" , "Gender" : 0, "age" : 18},
  3. {"Name": "Lisi", "Gender": 1, "age": 19 }
  4. ]

Then look at a protobuf file:

    1. Message person {
    2. Required string Name = 1;
    3. Optional int32 Gender = 2;
    4. Optional Int32 age = 3;
    5. }

The problem with JSON files is that there are too many invalid data, such as Name Gender , and so on, that the content is repeatedly repeated, making the data larger in size. Then look at the Protobuf file, which uses a unique ID (number) instead of the complex key in JSON, so as long as the data sender and the receiver are the same set of "template" to parse the data, it can greatly shorten the volume of the message.

Protobuf.js

Protobuf.js is a bytebuffer.js-based protocol buffers pure JavaScript implementation. The main functions are parsing .proto files, building the message class, and simply encoding and decoding. I am currently using the PROTOBUF format in a node-webkit for data interaction on the server side (the server is a PROTOBUF that is implemented according to the old C + + client requirements).

At present, the main use of protobuf.js is to read the old .proto file, create a message class, encode, send to the server, as follows:

user.protoFile:

  1. Package protobuf;
  2. Message Usermodel {
  3. Required string cyuserno = 1;
  4. Required string cypassword = 2;
  5. Required string cystatus = 3;
  6. }

Nodejs Code:

  1. var fs = require("FS"),
  2. protobuf = require("Protobufjs"),
  3. Userprotostr = fs. Readfilesync('./user.proto '). ToString(),
  4. Usermodel = protobuf. Loadproto(userprotostr). Build(' protobuf '). Usermodel,
  5. Usermodel;
  6. Usermodel= new Usermodel();
  7. Usermodel. Set(' Cyuserno ', ' 111111 ');
  8. Usermodel. Set(' Cypassword ', ' 123456 ');
  9. Usermodel. Set(' cystatus ', ' 1 ');
  10. var buffer = Usermodel. Encode(). Tobuffer();

The decoding method is also very simple:

    1. var userInfo = usermodel. Decode(data);
    2. UserInfo. Get(' Cyuserno ');
    3. ......

In addition Protobuf.js also provides some tools, such .proto as .json the conversion and so on, specific to his official website: https://github.com/dcodeIO/ProtoBuf.js

Finish

Protobuf.js–protocol buffers JavaScript implementation (RPM)

Related Article

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.