Skip to content

Commit e64cf65

Browse files
committed
Docs: Additional notes on the distinction of different use cases / distributions, see #666
1 parent 83758c9 commit e64cf65

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

README.md

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Contents
2222
* [Usage](#usage)<br />
2323
How to include protobuf.js in your project.
2424

25+
* [Distributions](#distributions)<br />
26+
A brief introduction to the available distributions and their use cases.
27+
2528
* [Examples](#examples)<br />
2629
A few examples to get you started.
2730

@@ -69,20 +72,31 @@ Production:
6972

7073
The `protobuf` namespace will always be available globally / also supports AMD loaders.
7174

72-
Additionally, the library is compiled in different versions. Which one to use depends on whether bundle size is a factor and your use case:
75+
Distributions
76+
-------------
77+
78+
The library supports both reflection-based and code-based use cases:
79+
80+
1. Parsing protocol buffer definitions (.proto files) to reflection
81+
2. Loading JSON descriptors to reflection
82+
3. Generating static code without any reflection features
7383

74-
| Build | Downloads | How to require | Description
75-
|---------|------------------------------|---------------------------------|-------------
76-
| 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
87+
|---------|---------|------------------------------|---------------------------------|-------------
88+
| 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.
7993

8094
Examples
8195
--------
8296

8397
### Using .proto files
8498

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:
86100

87101
```protobuf
88102
// awesome.proto
@@ -131,7 +145,9 @@ protobuf.load("awesome.proto")
131145

132146
### Using JSON descriptors
133147

134-
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:
135151

136152
```json
137153
// awesome.json
@@ -162,15 +178,16 @@ protobuf.load("awesome.json", function(err, root) {
162178
Or you can load it inline:
163179

164180
```js
165-
var root = protobuf.Root.fromJSON(descriptorJson);
181+
var jsonDescriptor = require("./awesome.json"); // exemplary for node
182+
183+
var root = protobuf.Root.fromJSON(jsonDescriptor);
166184

167185
// Continue at "Obtain a message type" above
168186
```
169187

170-
171188
### Using reflection only
172189

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:
174191

175192
```js
176193
...
@@ -218,11 +235,20 @@ AwesomeMessage.prototype.customInstanceMethod = function() { ... };
218235
* `AwesomeMessage.verify`
219236
* `AwesomeMessage.fromObject`, `AwesomeMessage.toObject`, `AwesomeMessage#toObject` and `AwesomeMessage#toJSON`
220237

221-
### Using the Reader/Writer interface directly
238+
### Using services
222239

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:
224241

225-
### Using services
242+
```js
243+
function rpcImpl(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:
247+
callback(null, responseData);
248+
}
249+
```
250+
251+
Example:
226252

227253
```protobuf
228254
// greeter.proto
@@ -260,17 +286,6 @@ greeter.sayHello({ name: 'you' })
260286
});
261287
```
262288

263-
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-
function rpcImpl(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-
274289
There is also an [example for streaming RPC](https://github.com/dcodeIO/protobuf.js/blob/master/examples/streaming-rpc.js).
275290

276291
### Usage with TypeScript

0 commit comments

Comments
 (0)