Connect
Establish a basic client connection to the KubeMQ server using the C# SDK to start sending and receiving messages.
Overview
Every KubeMQ application starts the same way: open a connection to the broker and prove it actually works before building anything on top of it. This tutorial is that first lesson — create a client, give it a stable identity, and confirm connectivity with a health check, so the pattern is muscle memory before you move on to real messaging.
KubeMQClientOptions is constructed with an Address and a ClientId — the ID tags this connection in broker logs, subscriptions, and management views, so pick something stable rather than a random string. new KubeMQClient(options) only builds the client; await client.ConnectAsync() opens the actual gRPC channel, and await client.PingAsync() verifies the round trip cheaply, returning live server info instead of just "no exception." The await using pattern calls DisposeAsync() automatically, releasing the channel even on exception.
Gotchas: constructing the client doesn't connect it — call ConnectAsync() explicitly before assuming connectivity; reusing the same client ID across running instances causes routing confusion on the broker; and forgetting the await using pattern in quick scripts is a common source of leaked channels under load.
Prerequisites
- KubeMQ server running on
localhost:50000 - C# SDK installed (
dotnet add package KubeMQ.SDK.CSharp)
Code
// KubeMQ .NET SDK — Connection: Connect
//
// This example demonstrates connecting to a KubeMQ server with explicit options.
//
// Prerequisites:
// - KubeMQ server running on localhost:50000
// - dotnet run
using KubeMQ.Sdk.Client;
var options = new KubeMQClientOptions
{
Address = "localhost:50000", // TODO: Replace with your KubeMQ server address
ClientId = "csharp-connection-connect-client",
};
await using var client = new KubeMQClient(options);
await client.ConnectAsync();
var info = await client.PingAsync();
Console.WriteLine($"Connected to {info.Host} v{info.Version}");
// Expected output:
// Connected to localhost v<version>
How It Works
KubeMQClientOptionsis constructed with anAddressand aClientId— the client ID uniquely identifies this connection on the broker.new KubeMQClient(options)creates the client; the actual gRPC channel is opened byawait client.ConnectAsync().await client.PingAsync()sends a lightweight health-check RPC and returns aServerInfoobject with the host name and server version.- The
await usingpattern ensuresDisposeAsync()is called automatically, closing the gRPC channel cleanly even on exception.
Related
- Getting started
- C# SDK Reference
- Close
- Ping
Was this page helpful?