Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 293
fbelanger Avatar asked May 26 '26 11:05

fbelanger


2 Answers

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

like image 137
fbelanger Avatar answered May 30 '26 05:05

fbelanger


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.

like image 27
theDmi Avatar answered May 30 '26 03:05

theDmi