KubeMQ
Client SDKsNode.jsTutorials

Send Your First Message

Connect the Node.js client to KubeMQ and publish and receive your first message end to end.

This is your first hands-on lesson with the Node.js SDK: create a client, send an event, and receive it. Make sure you have the SDK installed (see the Node.js SDK overview).

Create a Client

connect.ts
import { KubeMQClient } from 'kubemq-js';

const client = await KubeMQClient.create({ address: 'localhost:50000' });
console.log('Connected to KubeMQ');

Send Your First Event

send_event.ts
import { KubeMQClient, createEventMessage } from 'kubemq-js';

const client = await KubeMQClient.create({ address: 'localhost:50000' });

await client.sendEvent(
  createEventMessage({ channel: 'notifications', body: 'hello kubemq' }),
);
console.log('Event sent!');

Receive Events

receive_events.ts
client.subscribeToEvents({
  channel: 'notifications',
  onEvent: (msg) => console.log('Received:', new TextDecoder().decode(msg.body)),
  onError: (err) => console.error('Error:', err.message),
});

Configuration Options

OptionTypeDefaultDescription
addressstring(required)KubeMQ server address (host:port)
clientIdstringAuto-generated UUIDUnique client identifier
credentialsCredentialProvider | stringundefinedAuth token or credential provider
tlsTlsOptions | booleanSmart defaultTLS configuration
retryRetryPolicy3 retries, 500ms initialAuto-retry policy
reconnectReconnectionPolicyUnlimited, 500ms initialAuto-reconnection
connectionTimeoutSecondsnumber10Connection timeout
loggerLoggernoopLoggerStructured logging
tracerProviderunknownNo-opOpenTelemetry tracer provider
configuration.ts
import { KubeMQClient, createConsoleLogger } from 'kubemq-js';

const client = await KubeMQClient.create({
  address: 'kubemq-server:50000',
  credentials: 'my-auth-token',
  tls: { enabled: true, caCert: '/path/to/ca.pem' },
  retry: {
    maxRetries: 5,
    initialBackoffMs: 1000,
    maxBackoffMs: 30_000,
    multiplier: 2.0,
    jitter: 'full',
  },
  logger: createConsoleLogger('info'),
});

Error Handling

All SDK errors extend KubeMQError with 19 typed subclasses:

error_handling.ts
import { KubeMQError, ConnectionError, ValidationError } from 'kubemq-js';

try {
  await client.sendEvent(msg);
} catch (err) {
  if (err instanceof ConnectionError) {
    console.log('Server unreachable, will auto-retry');
  } else if (err instanceof ValidationError) {
    console.log('Fix the message:', err.suggestion);
  }
}

The SDK automatically retries transient errors (connection drops, timeouts, throttling) using exponential backoff with jitter.

Next Steps

Was this page helpful?

On this page