Skip to content

Commit 79d0ba2

Browse files
committed
Docs: Clarified that the service API targets clients consuming a service, see #742
1 parent 007b232 commit 79d0ba2

File tree

4 files changed

+57
-41
lines changed

4 files changed

+57
-41
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ AwesomeMessage.prototype.customInstanceMethod = function() { ... };
381381

382382
### Using services
383383

384-
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 request and a node-style callback as its parameters:
384+
The library also supports consuming 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 request and a node-style callback as its parameters:
385385

386386
```js
387387
function rpcImpl(method, requestData, callback) {
@@ -432,6 +432,8 @@ greeter.sayHello({ name: 'you' })
432432

433433
There is also an [example for streaming RPC](https://github.com/dcodeIO/protobuf.js/blob/master/examples/streaming-rpc.js).
434434

435+
Note that the service API is meant for clients. Implementing a server-side endpoint pretty much always requires transport channel (i.e. http, websocket, etc.) specific code with the only common denominator being that it decodes and encodes messages.
436+
435437
### Usage with TypeScript
436438

437439
The library ships with its own [type definitions](https://github.com/dcodeIO/protobuf.js/blob/master/index.d.ts) and modern editors like [Visual Studio Code](https://code.visualstudio.com/) should automatically detect and use them for code completion when following this pattern:

cli/README.md

+30-25
Original file line numberDiff line numberDiff line change
@@ -43,50 +43,55 @@ Usage
4343
```
4444
Translates between file formats and generates static code.
4545
46-
-t, --target Specifies the target format. Also accepts a path to require a custom target.
46+
-t, --target Specifies the target format. Also accepts a path to require a custom target.
4747
4848
json JSON representation
4949
json-module JSON representation as a module
5050
proto2 Protocol Buffers, Version 2
5151
proto3 Protocol Buffers, Version 3
52-
static Static code without reflection
52+
static Static code without reflection (non-functional on its own)
5353
static-module Static code without reflection as a module
5454
55-
-p, --path Adds a directory to the include path.
55+
-p, --path Adds a directory to the include path.
5656
57-
-o, --out Saves to a file instead of writing to stdout.
57+
-o, --out Saves to a file instead of writing to stdout.
5858
59-
Module targets only:
59+
--sparse Exports only those types referenced from a main file (experimental).
6060
61-
-w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.
61+
Module targets only:
6262
63-
default Default wrapper supporting both CommonJS and AMD
64-
commonjs CommonJS wrapper
65-
amd AMD wrapper
66-
es6 ES6 wrapper (implies --es6)
63+
-w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.
6764
68-
-r, --root Specifies an alternative protobuf.roots name.
65+
default Default wrapper supporting both CommonJS and AMD
66+
commonjs CommonJS wrapper
67+
amd AMD wrapper
68+
es6 ES6 wrapper (implies --es6)
6969
70-
-l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:
70+
-r, --root Specifies an alternative protobuf.roots name.
7171
72-
eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins
72+
-l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:
7373
74-
--es6 Enables ES6 syntax (const/let instead of var)
74+
eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins
7575
76-
Proto sources only:
76+
--es6 Enables ES6 syntax (const/let instead of var)
7777
78-
--keep-case Keeps field casing instead of converting to camel case.
78+
Proto sources only:
7979
80-
Static targets only:
80+
--keep-case Keeps field casing instead of converting to camel case.
8181
82-
--no-create Does not generate create functions used for reflection compatibility.
83-
--no-encode Does not generate encode functions.
84-
--no-decode Does not generate decode functions.
85-
--no-verify Does not generate verify functions.
86-
--no-convert Does not generate convert functions like from/toObject
87-
--no-delimited Does not generate delimited encode/decode functions.
88-
--no-beautify Does not beautify generated code.
89-
--no-comments Does not output any JSDoc comments.
82+
Static targets only:
83+
84+
--no-create Does not generate create functions used for reflection compatibility.
85+
--no-encode Does not generate encode functions.
86+
--no-decode Does not generate decode functions.
87+
--no-verify Does not generate verify functions.
88+
--no-convert Does not generate convert functions like from/toObject
89+
--no-delimited Does not generate delimited encode/decode functions.
90+
--no-beautify Does not beautify generated code.
91+
--no-comments Does not output any JSDoc comments.
92+
93+
--force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.
94+
--force-message Enfores the use of runtime messages instead of plain objects.
9095
9196
usage: pbjs [options] file1.proto file2.json ... (or) other | pbjs [options] -
9297
```

cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{}
1+
{"version": "6.7.0"}

examples/streaming-rpc.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this example demonstrates how to implement streaming rpc for services.
1+
// this example demonstrates how to consume a streaming rpc service.
22

33
/*eslint-disable strict, no-console*/
44
var protobuf = require("..");
@@ -53,20 +53,28 @@ var greeter = Greeter.create(/* rpcImpl */ (function() { // API documentation: S
5353
ended = true;
5454
return;
5555
}
56-
setTimeout(function() {
57-
try {
58-
// begin exemplary server side code
59-
var hello = Hello.decodeDelimited(requestData);
60-
var responseData = World.encodeDelimited({ message: "Hello " + hello.name }).finish();
61-
// end exemplary server side code
62-
return callback(null, responseData);
63-
} catch (err) {
64-
return callback(err);
65-
}
66-
}, Math.random() * 500);
56+
// in a real-world scenario, the client would now send requestData to a server using some
57+
// sort of transport layer (i.e. http), wait for responseData and call the callback.
58+
performRequestOverTransportChannel(requestData, function(responseData) {
59+
callback(null, responseData);
60+
});
6761
};
6862
})(), /* requestDelimited? */ true, /* responseDelimited? */ true);
6963

64+
// examplary server-side code for the sake of this example
65+
function performRequestOverTransportChannel(requestData, callback) {
66+
setTimeout(/* simulated delay */function() {
67+
// 1. server decodes the request
68+
var request = Hello.decodeDelimited(requestData);
69+
// 2. server handles the request and creates a response
70+
var response = { message: "Hello " + request.name };
71+
setTimeout(/* simulated delay */function() {
72+
// 3. server encodes and sends the response
73+
callback(World.encodeDelimited(response).finish());
74+
}, Math.random() * 250);
75+
}, Math.random() * 250);
76+
}
77+
7078
// Listen for events:
7179

7280
greeter.on("data", function(response, method) {
@@ -100,6 +108,7 @@ setTimeout(function() {
100108
greeter.end();
101109
// ^ Signals rpcImpl that the service has been ended client-side by calling it with a null buffer.
102110
// Likewise, rpcImpl can also end the stream by calling its callback with an explicit null buffer.
103-
104-
greeter.sayHello({ name: "three" }); // does nothing
111+
greeter.sayHello({ name: "three" }, function(err) {
112+
console.error("this should fail: " + err.message);
113+
});
105114
}, 501);

0 commit comments

Comments
 (0)