0

I am trying to do batch publishing of messages using node module @google-cloud/pubsub. My batch publishing code looks like below.

const { PubSub } = require("@google-cloud/pubsub");
const grpc = require("grpc");

const createPublishEventsInBatch = (fastify, topic) => {
  const pubSub = new PubSub({ grpc });
  const batchPublisher = pubSub.topic(topic, {
    batching: {
      maxMessages: 100,
      maxMilliseconds: 1000
    }
  });

  return (logTrace, data, eventInfo, version) => {
    const { entityType, eventType } = eventInfo;
    fastify.log.debug({
      logTrace,
      eventType: eventType,
      data,
      message: `Publishing batch events for ${entityType}`
    });

    const event = createEvent(data, entityType, eventType, logTrace, version);
    batchPublisher.publish(Buffer.from(JSON.stringify(event)));

    fastify.log.debug({
      traceHeaders: logTrace,
      tenant: data.tenant,
      message: "Event publish completed",
      data
    });
  };
}; 

Pubsub and gRPC version as follows.

"@google-cloud/pubsub": "^2.18.1",
"grpc": "^1.24.11"

When I am publishing the message with above code I am getting the following error.

(node:6) UnhandledPromiseRejectionWarning: TypeError: Channel credentials must be a ChannelCredentials object
    at new ChannelImplementation (/app/node_modules/@grpc/grpc-js/build/src/channel.js:75:19)
    at new Client (/app/node_modules/@grpc/grpc-js/build/src/client.js:61:36)
    at new ServiceClientImpl (/app/node_modules/@grpc/grpc-js/build/src/make-client.js:58:5)
    at GrpcClient.createStub (/app/node_modules/google-gax/build/src/grpc.js:334:22)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

I am seeing this issue only in my production environment and in staging and all my lower environment, this is working fine. Can somebody please guide me to fix this issue.

viz_tm
  • 115
  • 1
  • 1
  • 7
  • Could you try this workaround as mentioned in the [GitHub issue](https://github.com/firebase/firebase-tools/issues/2207#issuecomment-624116488) on your end i.e try to reinstall the modules in your production environment. – Sandeep Mohanty Oct 15 '21 at 05:21

1 Answers1

0

Not in regards to the exception, but I wanted to mention that you'd generally want to do this once and then cache:

  const pubSub = new PubSub({ grpc });
  const batchPublisher = pubSub.topic(topic, {

This lets you avoid a lot of init overhead, possibly some memory leaks (from proto parsing), and lets you keep a single publishing queue (and batching) for all requests.

feywind
  • 51
  • 3