Protobuf Use of Nodejs

Source: Internet
Author: User

use of Protobuf:

Project address:https://github.com/chrisdew/protobuf

Peed

the protobuf for node add-on relies  on the protobuf C++ runtime but it does not require  Any generation, compilation or linkage of generated c++ code. it  works reflectively and is thus able to deal with arbitrary  schemas. this means however, that it is not as fast as  with generated code. simple measurements show that unmarshalling  is about between 20% and 50% faster than v8 ' s native  Json support.

Calling C + + services is faster since it avoids marshalling to bytes but transfers data directly between the JS objects and The protocol messages. Also, in this case you compile the generated service interface to your code, so reflection is faster.

Building and Running

You need to has node. js 0.4 and Protobuf 2.4.0a installed. Protobuf_for_node depends on the respective versions of V8 and Eio shipped by node. Use branch Node2 for Node0.2/protobuf 2.3.0. If you want to use a later PROTOBUF version, your may need to re-generate The example Protoservice CODE:CD example; Protoc--cpp_out=. Protoservice.proto.

To build protobuf for node, do:

Protobuf=<protobuf prefix>/path/to/node/bin/node-waf Configure clean Build

This would build a) the Protobuf_for_node library (build/default/libprotobuf_for_node_lib.so). It is linked to by your service-exporting add-ons and by the B) Protobuf_for_node add-on (Build/default/protobuf_for_node. node).

Need a) and the Protobuf library in your dyld_library_path and b) in your node_path if you require (' Protobuf_for_node ' ).

1.

Need A *. Protofile that can generatedesc file is a command-line tool that uses Google:protoc--descriptor_set_out=buftest.desc--include_imports buftest.protogenerated by the. There was an error running, without this command-line tool. Find downloads, find inaccessible sites, need to set up proxy, after setting up the agent: specific tutorial in:http://dbua.iteye.com/blog/1633079,http://www.cnblogs.com/ anker/archive/2013/07/24/3209764.html, tested to generate desc file normally

*. ProtoThe basic file format is as follows:message Person {
Required String name = 1;
required Int32 id = 2;
ptional 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;
}

. proto file authoring rules:

P data types supported by Roto:http://blog.csdn.net/superbfly/article/details/17920383

1 debug mode buffer required

2) for optional fields, if not initialized, a default value is assigned to the field, and of course you can specify the default value, as described above proto in the definition Phonetype The field type.

    3) for repeated field, this field can be repeated multiple,Google provides this addressbook The example has a good scenario for the modifier, which means that each person may have multiple phone numbers. In high-level languages, we can use arrays to do this, and in proto repeated can be used in semantic files to achieve the same purpose. Of course, there are 0 of times that are included.       

where the field label indicates where the field is stored in the binary stream, this is necessary, and the Tag value of the same field must correspond when serializing and deserializing , otherwise the deserialization will cause unexpected problems.

P Rot File Compilation example:http://my.oschina.net/cxh3905/blog/293000

2. use in the project ' Protobuf ', combined with what we have written.Protobuffile-generateddesc file to serialize the data. (Successful with demo test)

Code:

var fs = require (' FS ');var Schema = require (' Protobuf '). Schema; //"schema" contains all message types defined in Buftest.proto|desc.var schema = new schema (Fs.readfilesync (' Buftest.desc ')); //The "buftest" message.var buftest = schema[' com.chrisdew.buftest.BufTest ']; var ob = {num:42};ob.payload = new Buffer ("Hello World"); var proto = Buftest.serialize (ob);Console.log (' proto.length: ', proto.length); var Outob = Buftest.parse (proto);Console.log (' unserialised: ', Json.stringify (Outob)); var payload = new Buffer (outob.payload);Console.log (payload);

If there is data transfer with the client, the client must also have a corresponding decoding file, in order to decode the data we sent past,AS3 Here is explained: http://bbs.9ria.com/thread-62331-1-1.html.

protobuf More detailed tutorials: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/


This article is from the "Jie" blog, make sure to keep this source http://zdok1024.blog.51cto.com/1656266/1539460

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.