google-cloud-platformgoogle-cloud-pubsubgcloud-nodegoogle-cloud-pubsub-emulator

GCloud pubsub emulator doesn't respect "PUBSUB_EMULATOR_HOST" environment variable


I tried running the pubsub emulator locally and send messages to it with existing services that I have running on pubsub. Messages weren't received, and all I get are auth errors in the logs.

[pubsub] Jan 22, 2017 3:43:16 PM com.google.cloud.iam.testing.v1.shared.authorization.AuthInterceptor interceptCall
[pubsub] INFO: Authentication interceptor: Header value is null
[pubsub] Jan 22, 2017 3:43:16 PM com.google.cloud.iam.testing.v1.shared.authorization.AuthInterceptor interceptCall
[pubsub] INFO: Authentication interceptor: invalid or missing token

I'm making requests to publish and pull from both dotnet and nodejs.

C#

var creds = GoogleCredential.GetApplicationDefaultAsync().Result;
if (creds.IsCreateScopedRequired) {
    creds = creds.CreateScoped(new [] { PubsubService.Scope.Pubsub } );
}

return new PubsubService(
    new BaseClientService.Initializer() {
        HttpClientInitializer = creds,
        ApplicationName = "My Wonderful App"
    }
);

NodeJs

var pubsub = require('@google-cloud/pubsub');

var pubsubClient = pubsub({
  projectId: config.get('GCLOUD_PROJECT')
});

Solution

  • The header value null was a red harring. It looks like the dotnet sdk doesn't respect the environment variable like the nodejs sdk does. I corresponded with jskeet and he created an issue to add the documentation for showing how to enable the use of the emulator: https://github.com/GoogleCloudPlatform/google-cloud-dotnet/issues/859

    Here's how I created the PublisherClient in C#

    private static PublisherClient CreatePublisherClient() {
        var emulatorHostAndPort = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
        if (String.IsNullOrWhiteSpace(emulatorHostAndPort)) {
            return PublisherClient.Create();
        } else {
            var channel = new Channel(emulatorHostAndPort, ChannelCredentials.Insecure);
            return PublisherClient.Create(channel);
        }
    }