You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| full | [dist][dist-full] | `require("protobufjs")` | All features. Works with everything.
77
-
| light | [dist/light][dist-light] | `require("protobufjs/light")` | All features except tokenizer, parser and bundled common types. Works with reflection, JSON definitions and static code.
78
-
| minimal | [dist/minimal][dist-minimal] | `require("protobufjs/minimal")` | Just enough to run statically generated code. Works with static code only.
84
+
There is a suitable distribution for each of these:
85
+
86
+
| Build | GZ Size | Downloads | How to require | Description
| full | 18.5kb | [dist][dist-full] | `require("protobufjs")` | All features. Works with everything.
89
+
| light | 15.5kb | [dist/light][dist-light] | `require("protobufjs/light")` | All features except tokenizer, parser and bundled common types. Works with JSON definitions, pure reflection and static code.
90
+
| minimal | 6.0kb+ | [dist/minimal][dist-minimal] | `require("protobufjs/minimal")` | Just enough to run static code - and nothing else.
91
+
92
+
In case of doubt you can just use the full library.
79
93
80
94
Examples
81
95
--------
82
96
83
97
### Using .proto files
84
98
85
-
It's super easy to load an existing .proto file using the full build, which parses and compiles the definitions to ready to use runtime message classes:
99
+
It's possible to load existing .proto files using the full library, which parses and compiles the definitions to ready to use runtime message classes:
The library utilizes a JSON format that is equivalent to a .proto definition (see also: [Command line usage](#command-line)). The following is identical to the .proto definition seen above:
148
+
The library utilizes a JSON format that is equivalent to a .proto definition (see also: [Command line usage](#command-line)).
149
+
150
+
The following is identical to the .proto definition seen above, but it can also be used with just the light library because it doesn't require the parser:
var jsonDescriptor =require("./awesome.json"); // exemplary for node
182
+
183
+
var root =protobuf.Root.fromJSON(jsonDescriptor);
166
184
167
185
// Continue at "Obtain a message type" above
168
186
```
169
187
170
-
171
188
### Using reflection only
172
189
173
-
Both the full and the light build include full reflection support. You could, for example, define the .proto definitions seen in the example above using just reflection:
190
+
Both the full and the light library include full reflection support. You could, for example, define the .proto definitions seen in the examples above using just reflection:
*`AwesomeMessage.fromObject`, `AwesomeMessage.toObject`, `AwesomeMessage#toObject` and `AwesomeMessage#toJSON`
220
237
221
-
### Using the Reader/Writer interface directly
238
+
### Using services
222
239
223
-
While only useful for the adventurous cherishing an aversion to [generated static code](https://github.com/dcodeIO/protobuf.js#command-line), it's also possible to use the Reader/Writer interface directly depending just on the [minimal library][dist-minimal] ([basic example](https://github.com/dcodeIO/protobuf.js/blob/master/examples/reader-writer.js)). Easy ways to obtain example code snippets are either setting `protobuf.util.codegen.verbose = true` while watching the console as code generation happens, or simply inspecting generated static code.
240
+
The library also supports services but it doesn't make any assumptions about the actual transport channel. Instead, a user must provide a suitable RPC implementation, which is an asynchronous function that takes the reflected service method, the binary HelloRequest and a node-style callback as its parameters:
224
241
225
-
### Using services
242
+
```js
243
+
functionrpcImpl(method, requestData, callback) {
244
+
// perform the request using an HTTP request or a WebSocket for example
245
+
var responseData =...;
246
+
// and call the callback with the binary response afterwards:
To make this work, all you have to do is provide an `rpcImpl`, which is an asynchronous function that takes the reflected service method, the binary HelloRequest and a node-style callback as its parameters. For example:
264
-
265
-
```js
266
-
functionrpcImpl(method, requestData, callback) {
267
-
// perform the request using an HTTP request or a WebSocket for example
268
-
var responseData =...;
269
-
// and call the callback with the binary response afterwards:
270
-
callback(null, responseData);
271
-
}
272
-
```
273
-
274
289
There is also an [example for streaming RPC](https://github.com/dcodeIO/protobuf.js/blob/master/examples/streaming-rpc.js).
0 commit comments