KubeMQ
Client SDKsC#Tutorials

Send Your First Message

Connect the C# client to KubeMQ and publish and receive your first message end to end.

This is your first hands-on lesson with the C# SDK: create a client, send an event, and receive it. Make sure you have the SDK installed (see the C# SDK overview).

Create a Client

Connect.cs
using KubeMQ.Sdk.Client;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

Console.WriteLine("Connected to KubeMQ");

Send Your First Event

SendEvent.cs
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Events;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

await client.SendEventAsync(new EventMessage
{
    Channel = "notifications",
    Body = Encoding.UTF8.GetBytes("hello kubemq")
});
Console.WriteLine("Event sent!");

Receive Events

ReceiveEvents.cs
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Events;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

await foreach (var msg in client.SubscribeToEventsAsync(
    new EventsSubscription { Channel = "notifications" }))
{
    Console.WriteLine($"Received: {Encoding.UTF8.GetString(msg.Body.Span)}");
}

Configuration Options

using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Config;

var client = new KubeMQClient(new KubeMQClientOptions
{
    Address = "kubemq-server:50000",
    ClientId = "my-service",
    DefaultTimeout = TimeSpan.FromSeconds(10),
    Tls = new TlsOptions { Enabled = true, CaFile = "/certs/ca.pem" },
    Retry = new RetryPolicy { MaxRetries = 5 },
});
OptionTypeDefaultDescription
Addressstring"localhost:50000"KubeMQ server address
ClientIdstring?Auto-generatedUnique client identifier
AuthTokenstring?nullJWT authentication token
DefaultTimeoutTimeSpan5sDefault operation timeout
ConnectionTimeoutTimeSpan10sInitial connection timeout
WaitForReadybooltrueBlock during reconnection
TlsTlsOptions?nullTLS/mTLS configuration
RetryRetryPolicy3 retriesRetry with exponential backoff
KeepaliveKeepaliveOptions10s pinggRPC keepalive
ReconnectReconnectOptionsUnlimitedAuto-reconnection
LoggerFactoryILoggerFactory?nullStructured logging

ASP.NET Core / Dependency Injection

Program.cs
builder.Services.AddKubeMQ(opts =>
{
    opts.Address = "kubemq-server:50000";
});

Or bind from configuration:

builder.Services.AddKubeMQ(builder.Configuration);
appsettings.json
{
  "KubeMQ": {
    "Address": "kubemq-server:50000",
    "DefaultTimeout": "00:00:10"
  }
}

Error Handling

All SDK methods throw typed exceptions derived from KubeMQException:

ErrorHandling.cs
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Events;
using KubeMQ.Sdk.Exceptions;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

try
{
    await client.SendEventAsync(new EventMessage
    {
        Channel = "events",
        Body = Encoding.UTF8.GetBytes("hello")
    });
}
catch (KubeMQTimeoutException ex)
{
    Console.WriteLine($"Timeout: {ex.Message}");
}
catch (KubeMQAuthenticationException ex)
{
    Console.WriteLine($"Auth failed: {ex.Message}");
}
catch (KubeMQConnectionException ex)
{
    Console.WriteLine($"Connection lost: {ex.Message}");
}
catch (KubeMQException ex)
{
    Console.WriteLine($"Error [{ex.ErrorCode}]: {ex.Message}");
}

Next Steps

Was this page helpful?

On this page