diff --git a/README.md b/README.md index 7eca2d360..bf8e4f686 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,44 @@ var message = new AwesomeMessage({ awesomeField: "AwesomeString" }); Custom classes are automatically populated with static `encode`, `encodeDelimited`, `decode`, `decodeDelimited` and `verify` methods and reference their reflected type via the `$type` property. Note that there are no methods (just `$type`) on instances by default as method names might conflict with field names. +### Using services + +```protobuf +// greeter.proto +service Greeter { + rpc SayHello (HelloRequest) returns (HelloReply) {} +} + +message HelloRequest { + string name = 1; +} + +message HelloReply { + string message = 1; +} +``` + +```js +... +var Greeter = root.lookup("Greeter"); +var greeter = Greeter.create(rpcImpl); + +greeter.sayHello({ name: 'you' }, function(err, response) { + console.log('Greeting:', response.message); +}); +``` + +To make this work, all you have to do is provide an `rpcImpl`, which is is an asynchronous function that takes the reflected service method, the binary HelloRequest and a node-style callback as its parameters. For example: + +```js +function rpcImpl(method, requestData, callback) { + // perform the request using an HTTP request or a WebSocket for example + var responseData = ...; + // and call the callback with the binary response afterwards: + callback(null, responseData); +} +``` + ### Usage with TypeScript ```ts diff --git a/src/util.js b/src/util.js index 9d00a9ae2..b0954aa60 100644 --- a/src/util.js +++ b/src/util.js @@ -98,7 +98,11 @@ util.asPromise = asPromise; function fetch(path, callback) { if (!callback) return asPromise(fetch, util, path); - try { return eval(['req','uire'].join(''))("fs").readFile(path, "utf8", callback); } catch (e) { } // eslint-disable-line no-empty, no-eval + try { + // Hide this from webpack. There is probably another, better way. + return eval(['req','uire'].join(''))("fs") // eslint-disable-line no-eval + .readFile(path, "utf8", callback); + } catch (e) { } // eslint-disable-line no-empty var xhr = new XMLHttpRequest(); function onload() { if (xhr.status !== 0 && xhr.status !== 200)