Reputation: 219
I'm having an issue with the PubSub module for Node.js. I've created a local environment using Docker and the PubSub emulator. I'm able to publish a message and retrieve it using the asynchronous pull (as documented here: https://cloud.google.com/pubsub/docs/pull#asynchronous-pull). However when I'm trying to use the synchronous pull (https://cloud.google.com/pubsub/docs/pull#synchronous-pull) I keep having the following error:
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information. at GoogleAuth. (/var/code/node_modules/google-auth-library/build/src/auth/googleauth.js:167:23) at next (native) at fulfilled (/var/code/node_modules/google-auth-library/build/src/auth/googleauth.js:19:58) at process._tickCallback (internal/process/next_tick.js:109:7) (node:493) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 32)
This error happens whenever I'm trying to instantiate the v1 PublisherClient or SubscriberClient:
const pubsub = require('@google-cloud/pubsub');
new pubsub.v1.PublisherClient();
new pubsub.v1.SubscriberClient();
I feel like the v1 components do not work with the emulator but connect directly to the cloud services instead. Is there a way to make these clients connect to the emulator rather than the cloud? I can't find any... Thanks !
Upvotes: 1
Views: 2091
Reputation: 2255
The following TS sample seems to work for me.
import { PubSub, v1 } from "@google-cloud/pubsub";
import * as gax from "google-gax";
if (process.env.PUBSUB_EMULATOR_HOST) {
const pieces = process.env.PUBSUB_EMULATOR_HOST.split(":");
options = {
servicePath: pieces[0],
port: pieces[1],
isEmulator: true,
sslCreds: gax.grpc.credentials.createInsecure(),
};
}
const subClient = new v1.SubscriberClient(options);
Upvotes: 1
Reputation: 219
I found out how to solve this issue: both PublisherClient and SubscriberClient constructors have options that set the path to the emulator. The options are servicePath
and port
. You also need valid credentials to pass to the sslCreds
option, generated using the grpc
module. Below is a sample:
const grpc = require('grpc');
const subscriber = new pubsub.v1.SubscriberClient({
servicePath: 'path.to.your.emulator',
port: 8080, // port your emulator is running on (default is 443)
sslCreds: grpc.credentials.createInsecure()
});
Here is the complete answer: https://github.com/googleapis/nodejs-pubsub/issues/346
Upvotes: 3