Technology moves fast! ⚡ Don't get left behind.🚶 Subscribe to our mailing list to keep up with latest and greatest in open source projects! 🏆


Subscribe to our mailing list

protocol-buffers

Protocol Buffers for Node.js

Subscribe to updates I use protocol-buffers


Statistics on protocol-buffers

Number of watchers on Github 553
Number of open issues 25
Average time to close an issue about 1 month
Main language JavaScript
Average time to merge a PR 2 days
Open pull requests 6+
Closed pull requests 3+
Last commit over 1 year ago
Repo Created about 5 years ago
Repo Last Updated over 1 year ago
Size 149 KB
Organization / Authormafintosh
Contributors12
Page Updated
Do you use protocol-buffers? Leave a review!
View open issues (25)
View protocol-buffers activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating protocol-buffers for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

protocol-buffers

Protocol Buffers for Node.js

npm install protocol-buffers

build status dat

Usage

Assuming the following test.proto file exists

enum FOO {
  BAR = 1;
}

message Test {
  required float num  = 1;
  required string payload = 2;
}

message AnotherOne {
  repeated FOO list = 1;
}

Use the above proto file to encode/decode messages by doing

var protobuf = require('protocol-buffers')

// pass a proto file as a buffer/string or pass a parsed protobuf-schema object
var messages = protobuf(fs.readFileSync('test.proto'))

var buf = messages.Test.encode({
  num: 42,
  payload: 'hello world'
})

console.log(buf) // should print a buffer

To decode a message use Test.decode

var obj = messages.Test.decode(buf)
console.log(obj) // should print an object similar to above

Enums are accessed in the same way as messages

var buf = messages.AnotherOne.encode({
  list: [
    messages.FOO.BAR
  ]
})

Nested emums are accessed as properties on the corresponding message

var buf = message.SomeMessage.encode({
  list: [
    messages.SomeMessage.NESTED_ENUM.VALUE
  ]
})

See the Google Protocol Buffers docs for more information about the available types etc.

Compile to a file

Since v4 you can now compile your schemas to a JavaScript file you can require from Node. This means you do not have runtime parse the schemas, which is useful if using in the browser or on embedded devices. It also makes the dependency footprint a lot smaller.

# first install the cli tool
npm install -g protocol-buffers

# compile the schema
protocol-buffers test.proto -o messages.js

# then install the runtime dependency in the project
npm install --save protocol-buffers-encodings

That's it! Then in your application you can simply do

var messages = require('./messages')

var buf = messages.Test.encode({
  num: 42
})

Performance

This module is fast.

It uses code generation to build as fast as possible encoders/decoders for the protobuf schema. You can run the benchmarks yourself by doing npm run bench.

On my Macbook Air it gives the following results

Benchmarking JSON (baseline)
  Running object encoding benchmark...
  Encoded 1000000 objects in 2142 ms (466853 enc/s)

  Running object decoding benchmark...
  Decoded 1000000 objects in 970 ms (1030928 dec/s)

  Running object encoding+decoding benchmark...
  Encoded+decoded 1000000 objects in 3131 ms (319387 enc+dec/s)

Benchmarking protocol-buffers
  Running object encoding benchmark...
  Encoded 1000000 objects in 2089 ms (478698 enc/s)

  Running object decoding benchmark...
  Decoded 1000000 objects in 735 ms (1360544 dec/s)

  Running object encoding+decoding benchmark...
  Encoded+decoded 1000000 objects in 2826 ms (353857 enc+dec/s)

Note that JSON parsing/serialization in node is a native function that is really fast.

Leveldb encoding compatibility

Compiled protocol buffers messages are valid levelup encodings. This means you can pass them as valueEncoding and keyEncoding.

var level = require('level')
var db = level('db')

db.put('hello', {payload:'world'}, {valueEncoding:messages.Test}, function(err) {
  db.get('hello', {valueEncoding:messages.Test}, function(err, message) {
    console.log(message)
  })
})

License

MIT

protocol-buffers open issues Ask a question     (View All Issues)
  • almost 3 years Streams support?
  • about 3 years encoding an out-of-range integer results in a corrupt message
  • over 3 years Negative int32 values are not decoded properly.
  • over 3 years Enum Translations
  • over 3 years Unknown wire type: 6
  • over 3 years imports is non-functional
  • over 3 years represent bigints in a safe way
  • over 3 years Cannot reference type with package name
  • over 3 years Support for ascii protobuf representation
  • over 3 years The library should throw if a repeated field with non-primitive type is provided with [packed=true]
  • over 4 years Could not resolve message field imported
  • over 4 years negative int64 values handling is not compatible with reference Protobuf encoding
  • over 4 years "Error: Unexpected token: " when parsing empty schema file
  • over 4 years Handling of default values
  • over 4 years "Offline" interface precompilation
  • over 4 years Use closures instead of genfun?
  • over 4 years Doesn't support group in protocol definition
protocol-buffers open pull requests (View All Pulls)
  • Add option to support null as default value
  • Implements import syntax.
  • Add option to support null as default value
  • Updated default value handling for optional fields
  • Fixed protobuf imports with respect to root_dir
  • use bytebuffers package to work with int64 numbers
protocol-buffers questions on Stackoverflow (View All Questions)
  • Dumping protocol-buffers file to stdout to debug/investigate?
  • Scala protocol buffers compiler
  • Does REST supports protocol buffers
  • Generate both Java and Scala classes using Protocol Buffers in ScalaPB
  • spring mvc rest protocol buffers http 406 not acceptable error
  • Serializing/Deserializing a standalone integer using protocol buffers
  • Is there a definitive *nix command line tool for inspecting protocol buffers?
  • Protocol Buffers in Java: can we handle primitive arrays efficiently?
  • Google Cloud Datastore with and w/o Protocol Buffers
  • Protocol buffers inheritance
  • Biggest differences of Thrift vs Protocol Buffers?
  • Cleanly handling encrypted data in protocol buffers or similar
  • Google Protocol Buffers Vs JSON: C++ to C# communication
  • Which Node.js module should I install to be able to use google protocol buffers?
  • How to use protocol buffers?
  • Setting value of a nested, non-repeating Protocol Buffers message in Python
  • Google Protocol Buffers - JavaScript
  • Why is Protocol Buffers so much better than .NET binary serialization?
  • Is there a Protocol Buffers binding for Codegear C++ Builder 2007?
  • Parsing Protocol Buffers, written in Java and read in Python
  • Wire protocol buffers vs Cap'n proto - which one is better?
  • Parsing Protocol-Buffers without knowing the .proto
  • Are there C++ equivalents for the Protocol Buffers delimited I/O functions in Java?
  • ASP.NET WebApi with Protocol Buffers - Error handling
  • Is there a standard mapping between JSON and Protocol Buffers?
  • Protocol buffers, where to use them?
  • Are there any applications currently offering a Protocol Buffers API?
  • How to handle the generic type Object with protocol buffers, in the .proto file?
  • Pants: Generate Python from Protocol Buffers
  • Spring MVC 4 REST with protocol buffers not working
protocol-buffers list of languages used
Other projects in JavaScript