Google Protocol Buffers for JavaScript

4.0.2 · maintenance · verified Sun Apr 19

Protocol Buffers for JavaScript provides the runtime library for Google's efficient, language-agnostic, and extensible mechanism for serializing structured data. The current stable version is 4.0.2, with releases generally tied to upstream `protoc` compiler updates and critical bug fixes, focusing on maintaining compatibility. This package primarily serves as the runtime component, which is internally consumed by JavaScript files generated from `.proto` definitions using the `protoc` compiler and its `protoc-gen-js` plugin. It explicitly supports CommonJS-style and Closure-style imports for the generated code, but direct ES6 module support for these generated `.js` files is not yet implemented, often necessitating build tools like Webpack or Browserify for seamless browser integration. A key differentiator is its deep integration with the broader Protocol Buffers ecosystem, offering a robust and standardized solution for defining and exchanging structured data across different programming languages in JavaScript and Node.js applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Protocol Buffer message, compile it using `protoc` and `protoc-gen-js`, and then use the generated JavaScript classes to create, serialize, and deserialize messages, including a well-known type like Timestamp.

/*
First, ensure you have the `protoc` compiler installed and on your PATH.
Download a pre-built binary from https://github.com/protocolbuffers/protobuf/releases

Then install the necessary npm packages:
npm install google-protobuf @protocolbuffers/protoc-gen-js

Create a file named `example.proto`:
*/
// example.proto
/*
syntax = "proto3";
package mypackage;

message MyMessage {
  string name = 1;
  int32 id = 2;
  repeated string tags = 3;
}
*/

/*
Compile your .proto file using protoc:
protoc --js_out=import_style=commonjs,binary:. example.proto \
       --plugin=protoc-gen-js=$(npm root)/@protocolbuffers/protoc-gen-js/cli.js

This will generate `example_pb.js`.
Now, run this JavaScript code (e.g., `node quickstart.js`):
*/

const { MyMessage } = require('./example_pb'); // Import the generated message class
const { Timestamp } = require('google-protobuf/google/protobuf/timestamp_pb'); // Import a well-known type

// Create a new instance of MyMessage
const message = new MyMessage();
message.setName('Hello Protobuf World');
message.setId(42);
message.addTags('example');
message.addTags('typescript');

console.log('Original Message:', message.toObject());

// Serialize the message to a binary buffer
const binaryData = message.serializeBinary();
console.log('Serialized Binary Data:', binaryData);

// Deserialize the message from the binary buffer
const deserializedMessage = MyMessage.deserializeBinary(binaryData);

console.log('Deserialized Message Name:', deserializedMessage.getName());
console.log('Deserialized Message ID:', deserializedMessage.getId());
console.log('Deserialized Message Tags:', deserializedMessage.getTagsList());

// Demonstrate using a Timestamp well-known type
const now = new Date();
const timestamp = new Timestamp();
timestamp.fromDate(now); // Convert a JavaScript Date object to a Protobuf Timestamp

console.log('Current Timestamp (Protobuf format):', timestamp.toObject());

view raw JSON →