Skip to content

Commit 3098d69

Browse files
authored
Merge pull request #2474 from murgatroid99/import_examples
Import examples from core repository
2 parents 186718c + 204cd38 commit 3098d69

27 files changed

+4462
-0
lines changed

examples/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
gRPC in 3 minutes (Node.js)
2+
===========================
3+
4+
PREREQUISITES
5+
-------------
6+
7+
- `node`: This requires Node 8.13.0 or greater.
8+
9+
INSTALL
10+
-------
11+
12+
```sh
13+
$ # Get the gRPC repository
14+
$ export REPO_ROOT=grpc-node # REPO root can be any directory of your choice
15+
$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc-node $REPO_ROOT
16+
$ cd $REPO_ROOT
17+
18+
$ cd examples
19+
$ npm install
20+
```
21+
22+
TRY IT!
23+
-------
24+
25+
There are two ways to generate the code needed to work with protocol buffers in Node.js - one approach uses [Protobuf.js](https://github.com/dcodeIO/ProtoBuf.js/) to dynamically generate the code at runtime, the other uses code statically generated using the protocol buffer compiler `protoc`. The examples behave identically, and either server can be used with either client.
26+
27+
- Run the server
28+
29+
```sh
30+
$ # from this directory
31+
$ node ./helloworld/dynamic_codegen/greeter_server.js &
32+
$ # OR
33+
$ node ./helloworld/static_codegen/greeter_server.js &
34+
```
35+
36+
- Run the client
37+
38+
```sh
39+
$ # from this directory
40+
$ node ./helloworld/dynamic_codegen/greeter_client.js
41+
$ # OR
42+
$ node ./helloworld/static_codegen/greeter_client.js
43+
```
44+
45+
TUTORIAL
46+
--------
47+
You can find a more detailed tutorial in [gRPC Basics: Node.js][]
48+
49+
[gRPC Basics: Node.js]:https://grpc.io/docs/languages/node/basics
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
*
3+
* Copyright 2015 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
var PROTO_PATH = __dirname + '/../../protos/helloworld.proto';
20+
21+
var parseArgs = require('minimist');
22+
var grpc = require('@grpc/grpc-js');
23+
var protoLoader = require('@grpc/proto-loader');
24+
var packageDefinition = protoLoader.loadSync(
25+
PROTO_PATH,
26+
{keepCase: true,
27+
longs: String,
28+
enums: String,
29+
defaults: true,
30+
oneofs: true
31+
});
32+
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
33+
34+
function main() {
35+
var argv = parseArgs(process.argv.slice(2), {
36+
string: 'target'
37+
});
38+
var target;
39+
if (argv.target) {
40+
target = argv.target;
41+
} else {
42+
target = 'localhost:50051';
43+
}
44+
var client = new hello_proto.Greeter(target,
45+
grpc.credentials.createInsecure());
46+
var user;
47+
if (argv._.length > 0) {
48+
user = argv._[0];
49+
} else {
50+
user = 'world';
51+
}
52+
client.sayHello({name: user}, function(err, response) {
53+
console.log('Greeting:', response.message);
54+
});
55+
}
56+
57+
main();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
*
3+
* Copyright 2015 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
var PROTO_PATH = __dirname + '/../../protos/helloworld.proto';
20+
21+
var grpc = require('@grpc/grpc-js');
22+
var protoLoader = require('@grpc/proto-loader');
23+
var packageDefinition = protoLoader.loadSync(
24+
PROTO_PATH,
25+
{keepCase: true,
26+
longs: String,
27+
enums: String,
28+
defaults: true,
29+
oneofs: true
30+
});
31+
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
32+
33+
/**
34+
* Implements the SayHello RPC method.
35+
*/
36+
function sayHello(call, callback) {
37+
callback(null, {message: 'Hello ' + call.request.name});
38+
}
39+
40+
/**
41+
* Starts an RPC server that receives requests for the Greeter service at the
42+
* sample server port
43+
*/
44+
function main() {
45+
var server = new grpc.Server();
46+
server.addService(hello_proto.Greeter.service, {sayHello: sayHello});
47+
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
48+
server.start();
49+
});
50+
}
51+
52+
main();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This is the static code generation variant of the Hello World. Code in these examples is pre-generated using protoc and the Node gRPC protoc plugin, and the generated code can be found in various `*_pb.js` files. The command line sequence for generating those files is as follows (assuming that `protoc` and `grpc_node_plugin` are present, and starting in the directory which contains this README.md file):
2+
3+
```sh
4+
cd ../protos
5+
npm install -g grpc-tools
6+
grpc_tools_node_protoc --js_out=import_style=commonjs,binary:../helloworld/static_codegen/ --grpc_out=grpc_js:../helloworld/static_codegen/ helloworld.proto
7+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* Copyright 2015 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
var parseArgs = require('minimist');
20+
var messages = require('./helloworld_pb');
21+
var services = require('./helloworld_grpc_pb');
22+
23+
var grpc = require('@grpc/grpc-js');
24+
25+
function main() {
26+
var argv = parseArgs(process.argv.slice(2), {
27+
string: 'target'
28+
});
29+
var target;
30+
if (argv.target) {
31+
target = argv.target;
32+
} else {
33+
target = 'localhost:50051';
34+
}
35+
var client = new services.GreeterClient(target,
36+
grpc.credentials.createInsecure());
37+
var request = new messages.HelloRequest();
38+
var user;
39+
if (argv._.length > 0) {
40+
user = argv._[0];
41+
} else {
42+
user = 'world';
43+
}
44+
request.setName(user);
45+
client.sayHello(request, function(err, response) {
46+
console.log('Greeting:', response.getMessage());
47+
});
48+
}
49+
50+
main();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
*
3+
* Copyright 2015 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
var messages = require('./helloworld_pb');
20+
var services = require('./helloworld_grpc_pb');
21+
22+
var grpc = require('@grpc/grpc-js');
23+
24+
/**
25+
* Implements the SayHello RPC method.
26+
*/
27+
function sayHello(call, callback) {
28+
var reply = new messages.HelloReply();
29+
reply.setMessage('Hello ' + call.request.getName());
30+
callback(null, reply);
31+
}
32+
33+
/**
34+
* Starts an RPC server that receives requests for the Greeter service at the
35+
* sample server port
36+
*/
37+
function main() {
38+
var server = new grpc.Server();
39+
server.addService(services.GreeterService, {sayHello: sayHello});
40+
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
41+
server.start();
42+
});
43+
}
44+
45+
main();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// GENERATED CODE -- DO NOT EDIT!
2+
3+
// Original file comments:
4+
// Copyright 2015 gRPC authors.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
'use strict';
19+
var grpc = require('@grpc/grpc-js');
20+
var helloworld_pb = require('./helloworld_pb.js');
21+
22+
function serialize_helloworld_HelloReply(arg) {
23+
if (!(arg instanceof helloworld_pb.HelloReply)) {
24+
throw new Error('Expected argument of type helloworld.HelloReply');
25+
}
26+
return Buffer.from(arg.serializeBinary());
27+
}
28+
29+
function deserialize_helloworld_HelloReply(buffer_arg) {
30+
return helloworld_pb.HelloReply.deserializeBinary(new Uint8Array(buffer_arg));
31+
}
32+
33+
function serialize_helloworld_HelloRequest(arg) {
34+
if (!(arg instanceof helloworld_pb.HelloRequest)) {
35+
throw new Error('Expected argument of type helloworld.HelloRequest');
36+
}
37+
return Buffer.from(arg.serializeBinary());
38+
}
39+
40+
function deserialize_helloworld_HelloRequest(buffer_arg) {
41+
return helloworld_pb.HelloRequest.deserializeBinary(new Uint8Array(buffer_arg));
42+
}
43+
44+
45+
// The greeting service definition.
46+
var GreeterService = exports.GreeterService = {
47+
// Sends a greeting
48+
sayHello: {
49+
path: '/helloworld.Greeter/SayHello',
50+
requestStream: false,
51+
responseStream: false,
52+
requestType: helloworld_pb.HelloRequest,
53+
responseType: helloworld_pb.HelloReply,
54+
requestSerialize: serialize_helloworld_HelloRequest,
55+
requestDeserialize: deserialize_helloworld_HelloRequest,
56+
responseSerialize: serialize_helloworld_HelloReply,
57+
responseDeserialize: deserialize_helloworld_HelloReply,
58+
},
59+
};
60+
61+
exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService);

0 commit comments

Comments
 (0)