Create Channel
Programmatically create a KubeMQ messaging channel with the Node.js SDK, provisioning queues or pub/sub topics ahead of traffic.
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.
client.createChannel(name, type) registers a channel directly with the server, where type is one of 'events', 'events_store', 'queues', 'commands', or 'queries' — each mapping to a different persistence and delivery contract.
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 - Node.js SDK installed (
npm install kubemq-js)
Code
import { KubeMQClient } from 'kubemq-js';
async function main() {
const client = await KubeMQClient.create({
address: 'localhost:50000',
clientId: 'js-management-create-channel-client',
});
try {
const eventsChannel = 'js-management.create-channel-events';
await client.createChannel(eventsChannel, 'events');
console.log('Created events channel:', eventsChannel);
const queuesChannel = 'js-management.create-channel-queues';
await client.createChannel(queuesChannel, 'queues');
console.log('Created queues channel:', queuesChannel);
} finally {
await client.close();
}
}
main().catch(console.error);
How It Works
client.createChannel(name, type)provisions a channel on the server if it does not already exist; it is idempotent and safe to call on every startup.- The second argument specifies the channel type:
'events','events_store','queues','commands', or'queries'— each maps to a different persistence and delivery contract. - Creation is asynchronous but fast; the Promise resolves when the server confirms the channel is ready to accept messages.
Related
Was this page helpful?