Create Channel
Create KubeMQ channels of various types programmatically using the Elixir SDK admin API.
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.
KubeMQ.Client.create_channel/3 takes a client, a channel name, and a type atom (:events, :events_store, :commands, :queries, or :queues), registering the channel directly with the broker and returning :ok or {:error, %KubeMQ.Error{}}.
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
- KubeMQ server running on
localhost:50000 - Elixir SDK installed (
{:kubemq, "~> 1.0"}in mix.exs)
Code
{:ok, client} = KubeMQ.Client.start_link(address: "localhost:50000", client_id: "elixir-mgmt-create")
channels = [
{"elixir-mgmt.events-channel", :events},
{"elixir-mgmt.events-store-channel", :events_store},
{"elixir-mgmt.commands-channel", :commands},
{"elixir-mgmt.queries-channel", :queries},
{"elixir-mgmt.queues-channel", :queues}
]
for {name, type} <- channels do
case KubeMQ.Client.create_channel(client, name, type) do
:ok -> IO.puts("Created #{type} channel '#{name}'")
{:error, err} -> IO.puts("Failed to create '#{name}': #{err.message}")
end
end
IO.puts("\nAll channels created.")
KubeMQ.Client.close(client)How It Works
create_channel/3accepts a channel name and type atom- Supported types:
:events,:events_store,:commands,:queries,:queues - Returns
:okon success or{:error, %KubeMQ.Error{}}on failure
Related
Was this page helpful?