Node Quick Start

This guide gets you started with gRPC in Node with a simple working example.

Prerequisites

  • Node version 4.0.0 or higher

Download the example

You’ll need a local copy of the example code to work through this quick start.Download the example code from our GitHub repository (the following commandclones the entire repository, but you just need the examples for this quick startand other tutorials):

  1. # Clone the repository to get the example code
  2. $ git clone -b v1.28.1 https://github.com/grpc/grpc
  3. # Navigate to the dynamic codegen "hello, world" Node example:
  4. $ cd grpc/examples/node/dynamic_codegen
  5. # Install the example's dependencies
  6. $ npm install

Run a gRPC application

From the examples/node/dynamic_codegen directory:

  • Run the server:
  1. $ node greeter_server.js
  • From another terminal, run the client:
  1. $ node greeter_client.js

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on theserver for the client to call. Our gRPC service is defined using protocolbuffers; you can find out lots more about how to define a service in a .protofile ingRPC Basics: Node. For now all you needto know is that both the server and the client “stub” have a SayHello RPCmethod that takes a HelloRequest parameter from the client and returns aHelloReply from the server, and that this method is defined like this:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. }
  6. // The request message containing the user's name.
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. // The response message containing the greetings
  11. message HelloReply {
  12. string message = 1;
  13. }

Let’s update this so that the Greeter service has two methods. Editexamples/protos/helloworld.proto and update it with a new SayHelloAgainmethod, with the same request and response types:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. // Sends another greeting
  6. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
  7. }
  8. // The request message containing the user's name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

Remember to save the file!

Update and run the application

We now have a new service definition, but we still need to implement and callthe new method in the human-written parts of our example application.

Update the server

In the same directory, open greeter_server.js. Implement the new method likethis:

  1. function sayHello(call, callback) {
  2. callback(null, {message: 'Hello ' + call.request.name});
  3. }
  4. function sayHelloAgain(call, callback) {
  5. callback(null, {message: 'Hello again, ' + call.request.name});
  6. }
  7. function main() {
  8. var server = new grpc.Server();
  9. server.addService(hello_proto.Greeter.service,
  10. {sayHello: sayHello, sayHelloAgain: sayHelloAgain});
  11. server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
  12. server.start();
  13. }

Update the client

In the same directory, open greeter_client.js. Call the new method like this:

  1. function main() {
  2. var client = new hello_proto.Greeter('localhost:50051',
  3. grpc.credentials.createInsecure());
  4. client.sayHello({name: 'you'}, function(err, response) {
  5. console.log('Greeting:', response.message);
  6. });
  7. client.sayHelloAgain({name: 'you'}, function(err, response) {
  8. console.log('Greeting:', response.message);
  9. });
  10. }

Run!

Just like we did before, from the examples/node/dynamic_codegen directory:

  • Run the server:
  1. $ node greeter_server.js
  • From another terminal, run the client:
  1. $ node greeter_client.js

What’s next