# Create Channel (/sdks/cpp/how-to/management/create-channel)



## Overview [#overview]

KubeMQ auto-creates a channel the first time a client publishes or subscribes to it — convenient for prototyping, but a liability once channels are infrastructure you need to reason about. Pre-creating channels with the management API lets you provision topology *before* any producer or consumer connects: enforce naming conventions in a startup script, stand up the channels a service depends on as part of deployment, or fail fast if a required channel is missing instead of it silently springing into existence.

The generic `CreateChannel(name, type)` method registers a channel with the server using a type constant such as `kChannelTypeEvents` or `kChannelTypeQueues`; typed methods like `CreateEventsChannel()` do the same without one.

**Gotchas:** the call is idempotent for a matching name and type, so it's safe to call on every startup — but a channel's type is fixed at creation, and reusing the name with a *different* type fails rather than migrating it. Creation only registers the channel; it does not start a consumer, so a freshly created queue or events channel happily accepts messages with nothing yet reading them.

## Prerequisites [#prerequisites]

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

## Code [#code]

```cpp title="main.cc"
// Example: management/create_channel
//
// Demonstrates creating channels of different types (events, queues, etc.)
// using both generic and typed convenience methods.
//
// Channel: cpp-management.create-channel
// Client ID: cpp-management-create-channel-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-create-channel-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;

    // Create an events channel using generic method.
    auto status =
        client->CreateChannel("cpp-management.create-channel.events", kubemq::kChannelTypeEvents);
    if (!status.ok()) {
        std::cerr << "[ERROR] CreateChannel events: " << status.message() << std::endl;
    } else {
        std::cout << "[2] Created events channel: cpp-management.create-channel.events"
                  << std::endl;
    }

    // Create a queues channel using generic method.
    status =
        client->CreateChannel("cpp-management.create-channel.queues", kubemq::kChannelTypeQueues);
    if (!status.ok()) {
        std::cerr << "[ERROR] CreateChannel queues: " << status.message() << std::endl;
    } else {
        std::cout << "[3] Created queue channel: cpp-management.create-channel.queues" << std::endl;
    }

    // Create an events store channel using generic method.
    status =
        client->CreateChannel("cpp-management.create-channel.es", kubemq::kChannelTypeEventsStore);
    if (!status.ok()) {
        std::cerr << "[ERROR] CreateChannel events_store: " << status.message() << std::endl;
    } else {
        std::cout << "[4] Created events store channel: cpp-management.create-channel.es"
                  << std::endl;
    }

    // Typed convenience methods.
    status = client->CreateEventsChannel("cpp-management.typed.events");
    if (!status.ok()) {
        std::cerr << "[ERROR] CreateEventsChannel: " << status.message() << std::endl;
    } else {
        std::cout << "[5] Created events channel (typed): cpp-management.typed.events" << std::endl;
    }

    status = client->CreateQueuesChannel("cpp-management.typed.queues");
    if (!status.ok()) {
        std::cerr << "[ERROR] CreateQueuesChannel: " << status.message() << std::endl;
    } else {
        std::cout << "[6] Created queues channel (typed): cpp-management.typed.queues" << std::endl;
    }

    status = client->CreateCommandsChannel("cpp-management.typed.commands");
    if (!status.ok()) {
        std::cerr << "[ERROR] CreateCommandsChannel: " << status.message() << std::endl;
    } else {
        std::cout << "[7] Created commands channel (typed): cpp-management.typed.commands"
                  << 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 << "[8] Client closed" << std::endl;

    return 0;
}
```

## How It Works [#how-it-works]

* Creates channels using the generic `CreateChannel(name, type)` method.
* Uses channel type constants: `kChannelTypeEvents`, `kChannelTypeQueues`, `kChannelTypeEventsStore`.
* Also demonstrates typed convenience methods like `CreateEventsChannel()`, `CreateQueuesChannel()`.
* Channels are created idempotently -- creating an existing channel is a no-op.

## Related [#related]

* [C++ SDK Reference](/sdks/cpp/reference/client)
* [Delete Channel](/sdks/cpp/how-to/management/delete-channel)
* [List Channels](/sdks/cpp/how-to/management/list-channels)
