# Send Your First Message (/sdks/nodejs/tutorials/first-message)



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](/sdks/nodejs)).

## Create a Client [#create-a-client]

```typescript title="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-your-first-event]

```typescript title="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]

```typescript title="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 [#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       |

```typescript title="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 [#error-handling]

All SDK errors extend `KubeMQError` with 19 typed subclasses:

```typescript title="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 [#next-steps]

* [Node.js SDK Reference](/sdks/nodejs/reference) — full API documentation
* [Node.js SDK Examples](/sdks/nodejs/how-to) — complete examples for all patterns
* [GitHub Repository](https://github.com/kubemq-io/kubemq-js) — source code and issues
