fbelanger
fbelanger

Reputation: 3568

Google Cloud PubSub V1 using GCloud Emulator

I'm fighting with Google Docs for setting up Cloud PubSub with .NET using a PubSub emulator.

https://cloud.google.com/dotnet/docs/getting-started/using-pub-sub

https://cloud.google.com/pubsub/docs/publisher

https://cloud.google.com/pubsub/docs/emulator

Coming from a Rails background, I'm tasked to implement Cloud PubSub for a .NET product, running our google cloud on .NET Core, to enable it to publish.

Google::Cloud::Pubsub.new(project: project_id, emulator_host: emulator_host)

From the documentation using .NET, I keep coming back to the following:

PublisherServiceApiClient publisherClient = PublisherServiceApiClient.Create();
PublisherClient publisher = PublisherClient.Create(...)

However, the library used from the docs Google.Cloud.PubSub.V1 -Pre does not contain the definition.

'PublisherClient' does not contain a definition for 'Create'.

Instead, I get CreateAsync that takes in TopicName, PublisherClient.ClientCreationSettings and PublisherClient.Settings.

https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/api/Google.Cloud.PubSub.V1.PublisherClient.html

I noticed that PublisherServiceApiClient can take in a Channel, but I'm confused on how to get this going.

To conclude with an actual question, how does one currently implement Cloud PubSub with .NET for in cloud and then locally with emulator? Adding to that, am I using the wrong library or the wrong docs?

Any suggestions, pointers or piece of advice would be truly appreciated.

Upvotes: 3

Views: 1782

Answers (3)

HakonIngvaldsen
HakonIngvaldsen

Reputation: 384

You can use the EmulatorDetection property on the ClientCreationSettings using extension method .WithEmulatorDetection(EmulatorDetection.EmulatorOrProduction). Like this:

PublisherClient publisher = await PublisherClient.CreateAsync(
        topicName, 
        new PublisherClient.ClientCreationSettings()
            .WithEmulatorDetection(EmulatorDetection.EmulatorOrProduction));

This will work if you have the following environment variable for the local emulator endpoint: PUBSUB_EMULATOR_HOST=localhost:8085

(If you use Visual Studio you might have to restart VS for the environment variable to be detected)

In windows I had problems using the set PUBSUB_EMULATOR_HOST=localhost:8085 command, so I ended up adding it manually.

Details here: https://cloud.google.com/pubsub/docs/emulator

Extra tip: you can add topics directly to API using curl: curl -X PUT http://localhost:8085/v1/projects/my-project-name/topics/my-topic

Upvotes: 0

theDmi
theDmi

Reputation: 18034

To make the PublisherClient connect to a local emulator, you need to pass custom ServiceEndpoint and ChannelCredentials to CreateAsync:

var serviceEndpoint = new ServiceEndpoint(theEmulatorHost, theEmulatorPort);

var publisherClient = await PublisherClient.CreateAsync(
    topicName,
    new PublisherClient.ClientCreationSettings(credentials: ChannelCredentials.Insecure, serviceEndpoint: serviceEndpoint));

To switch to the real PubSub, just leave away the ClientCreationSettings.

Upvotes: 1

fbelanger
fbelanger

Reputation: 3568

I managed a solution that I am happy with.

Instead of using the PublisherClient, I went with using the PublisherServiceApiClient alone.

emulatorAddr = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
if (emulatorAddr != null)
{
    channel = new Channel(emulatorAddr, ChannelCredentials.Insecure);
    pub = PublisherServiceApiClient.Create(channel);
}
else
{
    pub = PublisherServiceApiClient.Create();
}

Which meant that publishing was slightly more involved then sending string to the PublisherClient, but overall not so bad.

PubsubMessage msg = new PubsubMessage
{
    Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(payload))
};

pub.PublishAsync(topic, new[]{ msg });

If the project is running in a Google Compute Engine, it will have default credentials. Otherwise, wether you're running an emulator locally or in docker you can define PUBSUB_EMULATOR_HOST.

What really helped was this https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/index.html

Upvotes: 3

Related Questions