KubeMQ
Client SDKsC++How-to guidesManagement

List Channels

List active KubeMQ channels with an optional search filter using the C++ SDK administration API to inspect them.

Overview

Listing channels turns the broker into a discoverable inventory instead of a black box — instead of hardcoding channel names everywhere, you ask the server what actually exists right now. That's exactly what monitoring dashboards, cleanup scripts, and "did my deployment create the channels it should have" checks need. It's read-only and has no effect on message flow, so it's safe to run against production at any time.

Under the hood, client->ListChannels(type, search) queries channels of a given type, and an optional search string narrows results server-side to matching names. Typed helpers like ListEventsChannels() and ListQueuesChannels() wrap the same call without the channel-type constant. Each ChannelInfo result carries the name, active-subscriber status, and subscriber count.

Gotchas: the search filter is a substring/prefix match, not a glob or regex — there's no wildcard syntax to anchor or exclude. Both calls return a Result<T>-style wrapper, so check .ok() before dereferencing — a failed call doesn't throw. And active status and counts are a snapshot at query time, so a channel can go idle a moment later.

Prerequisites

  • KubeMQ server running on localhost:50000
  • C++ SDK installed (vcpkg or CMake FetchContent)
  • C++17 compiler (GCC 9+, Clang 9+, MSVC 2019+)

Code

main.cc
// Example: management/list_channels
//
// Demonstrates listing channels with optional search filter, using both
// generic and typed convenience methods.
//
// Channel: cpp-management.list-channels
// Client ID: cpp-management-list-channels-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).

#include <kubemq/kubemq.h>

#include <iostream>

int main() {
    std::cout << "[1] Connecting to localhost:50000" << std::endl;

    kubemq::ClientOptions options;
    options.set_address("localhost", 50000);
    options.set_client_id("cpp-management-list-channels-client");

    auto client_result = kubemq::Client::Create(options);
    if (!client_result.ok()) {
        std::cerr << "[ERROR] Failed to create client: " << client_result.status().message()
                  << std::endl;
        return 1;
    }
    auto& client = *client_result;

    // List all events channels.
    auto channels_result = client->ListChannels(kubemq::kChannelTypeEvents, "");
    if (!channels_result.ok()) {
        std::cerr << "[ERROR] ListChannels: " << channels_result.status().message() << std::endl;
    } else {
        std::cout << "[2] All events channels: " << channels_result->size() << " found"
                  << std::endl;
        for (const auto& ch : *channels_result) {
            std::cout << "  - name=" << ch.name << " active=" << ch.is_active << std::endl;
        }
    }

    // List channels with a search filter.
    auto filtered_result = client->ListChannels(kubemq::kChannelTypeQueues, "cpp-");
    if (!filtered_result.ok()) {
        std::cerr << "[ERROR] ListChannels filtered: " << filtered_result.status().message()
                  << std::endl;
    } else {
        std::cout << "[3] Queue channels matching 'cpp-': " << filtered_result->size() << " found"
                  << std::endl;
        for (const auto& ch : *filtered_result) {
            std::cout << "  - name=" << ch.name << std::endl;
        }
    }

    // Typed convenience methods.
    auto events_result = client->ListEventsChannels("");
    if (!events_result.ok()) {
        std::cerr << "[ERROR] ListEventsChannels: " << events_result.status().message()
                  << std::endl;
    } else {
        std::cout << "[4] Events channels (typed): " << events_result->size() << " found"
                  << std::endl;
        for (const auto& ch : *events_result) {
            std::cout << "  - name=" << ch.name << " active=" << ch.is_active << std::endl;
        }
    }

    auto queues_result = client->ListQueuesChannels("cpp-");
    if (!queues_result.ok()) {
        std::cerr << "[ERROR] ListQueuesChannels: " << queues_result.status().message()
                  << std::endl;
    } else {
        std::cout << "[5] Queue channels (typed, matching 'cpp-'): " << queues_result->size()
                  << " found" << std::endl;
        for (const auto& ch : *queues_result) {
            std::cout << "  - name=" << ch.name << std::endl;
        }
    }

    // Close the client explicitly.
    // Note: Client destructor would also handle cleanup (RAII)
    auto close_status = client->Close();
    if (!close_status.ok()) {
        std::cerr << "[ERROR] Close failed: " << close_status.message() << std::endl;
        return 1;
    }
    std::cout << "[6] Client closed" << std::endl;

    return 0;
}

How It Works

  • Lists all channels of a type with ListChannels(type, search).
  • An empty search string returns all channels; a prefix filters results.
  • Returns ChannelInfo with name, active status, and subscriber count.
  • Also demonstrates typed methods like ListEventsChannels() and ListQueuesChannels().

Was this page helpful?

On this page