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
using KubeMQ.Sdk.Client;
await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();
Console.WriteLine("Connected to KubeMQ");Send Your First Event
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
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 },
});| Option | Type | Default | Description |
|---|---|---|---|
Address | string | "localhost:50000" | KubeMQ server address |
ClientId | string? | Auto-generated | Unique client identifier |
AuthToken | string? | null | JWT authentication token |
DefaultTimeout | TimeSpan | 5s | Default operation timeout |
ConnectionTimeout | TimeSpan | 10s | Initial connection timeout |
WaitForReady | bool | true | Block during reconnection |
Tls | TlsOptions? | null | TLS/mTLS configuration |
Retry | RetryPolicy | 3 retries | Retry with exponential backoff |
Keepalive | KeepaliveOptions | 10s ping | gRPC keepalive |
Reconnect | ReconnectOptions | Unlimited | Auto-reconnection |
LoggerFactory | ILoggerFactory? | null | Structured logging |
ASP.NET Core / Dependency Injection
builder.Services.AddKubeMQ(opts =>
{
opts.Address = "kubemq-server:50000";
});Or bind from configuration:
builder.Services.AddKubeMQ(builder.Configuration);{
"KubeMQ": {
"Address": "kubemq-server:50000",
"DefaultTimeout": "00:00:10"
}
}Error Handling
All SDK methods throw typed exceptions derived from KubeMQException:
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
- C# SDK Reference — full API documentation
- C# SDK Examples — complete examples for all patterns
- GitHub Repository — source code and issues
Was this page helpful?