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
import { KubeMQClient } from 'kubemq-js';
const client = await KubeMQClient.create({ address: 'localhost:50000' });
console.log('Connected to KubeMQ');Send Your First Event
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
client.subscribeToEvents({
channel: 'notifications',
onEvent: (msg) => console.log('Received:', new TextDecoder().decode(msg.body)),
onError: (err) => console.error('Error:', err.message),
});Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
address | string | (required) | KubeMQ server address (host:port) |
clientId | string | Auto-generated UUID | Unique client identifier |
credentials | CredentialProvider | string | undefined | Auth token or credential provider |
tls | TlsOptions | boolean | Smart default | TLS configuration |
retry | RetryPolicy | 3 retries, 500ms initial | Auto-retry policy |
reconnect | ReconnectionPolicy | Unlimited, 500ms initial | Auto-reconnection |
connectionTimeoutSeconds | number | 10 | Connection timeout |
logger | Logger | noopLogger | Structured logging |
tracerProvider | unknown | No-op | OpenTelemetry tracer provider |
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:
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
- Node.js SDK Reference — full API documentation
- Node.js SDK Examples — complete examples for all patterns
- GitHub Repository — source code and issues
Was this page helpful?