2

Note that this is not a duplicate of a similar question for go, since this uses grpc-node. For some reason, there seems to be differences in the API

I do the standard procedure of creating my APIPackageDefinitions and APIPackagePbjects, and create two separate clients from each one, individually.

let grpc = require('grpc')
let protoLoader = require('@grpc/proto-loader')

async function createGrcpConnection() {
  const HOST = 'localhost'
  const PORT = '50053'
  const PORT2 = '50054'

  let physicalProjectAPIPackageDefinition = await protoLoader.load(
    './physical_project_api.proto',protoLoaderOptions
  )
  let configAPIPackageDefinition = await protoLoader.load(
    './config_api.proto', protoLoaderOptions
  )

  let physicalProjectAPIPackageObject = grpc.loadPackageDefinition(
    physicalProjectAPIPackageDefinition
  ).package.v1
  let configAPIPackageObject = grpc.loadPackageDefinition(
    configAPIPackageDefinition
  ).package.v1


  let grpcClient1 = physicalProjectAPIPackageObject.PhysicalProjectAPI(
    `${HOST}:${PORT}`,
    grpc.credentials.createInsecure()
  )
  let grpcClient2 = configAPIPackageObject.ConfigAPI(
    `${HOST}:${PORT2}`,
    grpc.credentials.createInsecure()
  )

  return { grpcClient1, grpcClient2 }
}

I am looking for a way to create two clients that share the same connection. I think I am close to the solution by creating a new Channel and replacing the last two let statements with

let cc = new grpc.Channel(
  `${HOST}:${PORT}`,
  grpc.credentials.createInsecure()
)

let grpcClient1 = physicalProjectAPIPackageObject.PhysicalProjectAPI(cc)
let grpcClient2 = configAPIPackageObject.ConfigAPI(cc)

However, I received a TypeError: Channel's first argument (address) must be a string. I'm not sure how to incorporate the newly instantiated Channel to create new clients for each service. I couldn't find any useful methods on the docs. Any help would be appreciated.

P.S. At the moment I am trying to use two services, and create a client for each service, and have those two clients share a connection on the same channel. Is it possible to use two service, and create a single client for both services? Maybe I can use .proto package namespaces to my advantage here? My internet search fu failed me on this question.

hyperupcall
  • 869
  • 10
  • 21

1 Answers1

2

There is an API to do this, but it is a bit more awkward than what you were trying. And you don't actually need to use it to get what you want. The grpc library internally pools connections to the same server, as long as those connections were created with identical parameters. So, the Client objects created in your first code block will actually use the same TCP connection.

However, as mentioned, there is a way to do this explicitly. The third argument to the Client constructor is an optional object with various additional options, including channelOverride. That accepts a Channel object like the one you constructed at the beginning of your second code block. You still have to pass valid values for the first two arguments, but they will actually be ignored and the third argument will be used instead. You can see more information about that constructor's arguments in the API documentation.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95