KubeMQ
Client SDKsC#How-to guidesEvents

Multiple Subscribers

Deliver the same KubeMQ events to multiple subscribers using the C# SDK fan-out pub/sub.

Overview

Two delivery models, one page

This page is the overview of subscribing more than one consumer to the same channel. KubeMQ Events give you two distinct delivery models — pick the one your scenario needs:

  • Fan-Out — broadcast: every subscriber gets its own copy of each event.
  • Consumer Group — load-balance: subscribers sharing a group name split events among themselves.

When multiple consumers subscribe to the same channel, which delivery model you get is controlled by one property: Group on EventsSubscription. Subscribers that set the same Group value are treated as one logical worker pool, so the broker routes each event to only one member. Leaving Group unset (or giving each subscriber a distinct value) switches to broadcast — every subscriber receives every event. The example below shows both subscribers sharing a group so you can see the load-balance side in action; see the linked pages above for the full write-up of each mode.

Gotchas: Events pub/sub has no durability — a subscriber that hasn't finished subscribing yet, or that disconnects, simply misses events published in that window; there's no redelivery. Which group member receives a given event depends on broker scheduling, so don't assume an even split. And accidentally reusing a Group value across subscribers meant to be independent silently turns fan-out into load-balancing.

Prerequisites

  • KubeMQ server running on localhost:50000
  • C# SDK installed (dotnet add package KubeMQ.SDK.CSharp)

Code

Program.cs
// KubeMQ .NET SDK — Events: Multiple Subscribers with Group Load Balancing
//
// This example demonstrates multiple subscribers on the same channel using groups.
// When subscribers share a group, messages are load-balanced among them.
// Without a group, all subscribers receive every message.
//
// Prerequisites:
//   - KubeMQ server running on localhost:50000
//   - dotnet run

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

await using var client = new KubeMQClient(new KubeMQClientOptions
{
    ClientId = "csharp-events-multiple-subscribers-client",
});
await client.ConnectAsync();

Console.WriteLine("Connected to KubeMQ server");

var cts = new CancellationTokenSource();

// Two subscribers in the same group — messages load-balanced
var sub1 = Task.Run(async () =>
{
    await foreach (var msg in client.SubscribeToEventsAsync(
        new EventsSubscription { Channel = "csharp-events.multiple-subscribers", Group = "workers" }, cts.Token))
    {
        Console.WriteLine($"[Worker-1] {Encoding.UTF8.GetString(msg.Body.Span)}");
    }
});

var sub2 = Task.Run(async () =>
{
    await foreach (var msg in client.SubscribeToEventsAsync(
        new EventsSubscription { Channel = "csharp-events.multiple-subscribers", Group = "workers" }, cts.Token))
    {
        Console.WriteLine($"[Worker-2] {Encoding.UTF8.GetString(msg.Body.Span)}");
    }
});

await Task.Delay(1000);

// Publish 6 events — distributed between the two workers
for (var i = 1; i <= 6; i++)
{
    await client.SendEventAsync(new EventMessage
    {
        Channel = "csharp-events.multiple-subscribers",
        Body = Encoding.UTF8.GetBytes($"Task #{i}")
    });
}

await Task.Delay(2000);
cts.Cancel();

Console.WriteLine("Done.");

How It Works

  • Both sub1 and sub2 use Group = "workers". The broker distributes events across the group so each event reaches exactly one subscriber — not both.
  • Task.Run wraps each await foreach in a thread-pool task so the two subscriptions can drain concurrently on the same client connection.
  • Removing the Group property switches to broadcast: every subscriber receives every event, doubling delivery.
  • Six events are sent and the output will show roughly 3 lines per worker, though the exact split depends on broker scheduling.

Was this page helpful?

On this page