node.jsstreamgrpcbidi

GRPC bidi stream configuration


I'm following this GRPC introduction video to implement a simple bidi stream between the client and the server

Probably I'm missing something in the implementation and actually I receive back this error:

Error: 14 UNAVAILABLE: GOAWAY received

This is my code:

-Proto:

syntax = "proto3";
package nbsb;

message EchoMessage {
  string value = 1;
  int32 value2 = 2;
}

service EchoService {
  rpc Echo (EchoMessage) returns (EchoMessage);

  rpc EchoClientStream (stream EchoMessage) returns (EchoMessage);

  rpc EchoServerStream (EchoMessage) returns (stream EchoMessage);

  rpc EchoBidiStream (stream EchoMessage) returns (stream EchoMessage);
}

-Server

var PROTO_PATH = __dirname + '/echo.proto';

var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });

var hello_proto = grpc.loadPackageDefinition(packageDefinition).nbsb;
const { Server } = require('grpc-server-js');

function main() {
    const server = new Server();
    server.addService(hello_proto.EchoService.service, {
        EchoBidiStream(stream){
            stream.on('error', console.error);
            stream.on('data', (data)=>{stream.write(data)});
            stream.on('end',()=>stream.end());
            }

    });


      server.bind('localhost:55555');
  }

  main();

-Client:

var PROTO_PATH = __dirname + '/echo.proto';

var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(PROTO_PATH, {keepCase: true, longs: String, enums: String, defaults: true, oneofs: true});
var hello_proto = grpc.loadPackageDefinition(packageDefinition).nbsb;

function main() {
  var client = new hello_proto.EchoService('localhost:55555',grpc.credentials.createInsecure());

    const stream = client.EchoBidiStream();

    stream.on('error', console.error);
    stream.on('data',console.log);
    stream.on('end',()=>client.close());


    stream.write({data:{value: "hello", value2:1}});
    stream.end({});

}

main()

The server implementation is grpc-server-js

Am I missing something or there is something wrong??

Thanks for the help!


Solution

  • You need to call server.start() after successfully binding the port to actually start handling requests. server.bind returns a promise, so you should either await that promise or use promise.then to know when binding is successful.