# Command Timeout (/sdks/nodejs/how-to/rpc/command-timeout)



## Overview [#overview]

A **command timeout** is the deadline you attach to a single RPC call so a caller never blocks forever waiting on a handler that isn't there or isn't responding. Commands are synchronous by design — the sender is parked until a reply arrives — so without a bound, a missing subscriber or a crashed handler turns one request into an indefinite hang that ties up a promise and cascades into upstream timeouts.

The timeout is set per call with the `timeoutInSeconds` option on `createCommand`, and it's enforced by the broker itself, not by client-side polling: the broker tracks the deadline server-side and fails the request the moment it expires, regardless of what's happening in your event loop. When the window elapses with no response, `sendCommand` rejects with a `KubeMQTimeoutError` — a specific type you can catch separately from connection failures you'd want to re-throw.

**Gotchas:** a command timeout is a broker-enforced deadline, not a local `setTimeout` or `AbortController` signal, so don't assume a client-side abort implies the broker also gave up; a slow-but-alive handler and a completely absent one produce the *same* `KubeMQTimeoutError`, so you can't tell them apart from the error alone; and setting the timeout too short under normal load turns transient latency into false failures.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* Node.js SDK installed (`npm install kubemq-js`)

## Code [#code]

```typescript title="command-timeout.ts"
import { KubeMQClient, createCommand, KubeMQTimeoutError } from 'kubemq-js';

async function main() {
  const client = await KubeMQClient.create({
    address: 'localhost:50000',
    clientId: 'js-rpc-command-timeout-client',
  });

  try {
    const resp = await client.sendCommand(
      createCommand({ channel: 'js-rpc.command-timeout', body: 'ping', timeoutInSeconds: 1 }),
    );
    console.log('Executed:', resp.executed);
  } catch (err) {
    if (err instanceof KubeMQTimeoutError) {
      console.log('Command timed out after 1s — no handler responded');
    } else {
      throw err;
    }
  } finally {
    await client.close();
  }
}

main().catch(console.error);

```

## How It Works [#how-it-works]

* `timeoutInSeconds: 1` sets a 1-second deadline on the command; the server enforces this and rejects the call if no handler responds in time.
* This example intentionally runs with no handler registered on `js-rpc.command-timeout`, guaranteeing the timeout path is exercised.
* `KubeMQTimeoutError` is the specific error type thrown when a command or query exceeds its timeout — catch it separately from other errors to implement retry or fallback logic.
* `throw err` in the else branch re-raises unexpected errors (connection failures, serialization errors) so they are not silently swallowed.

## Related [#related]

* [Pattern overview](/learn/rpc/getting-started)
* [Node.js SDK Reference](/sdks/nodejs/reference)
* [Send Command](/sdks/nodejs/tutorials/command-send)
* [Handle Command](/sdks/nodejs/how-to/rpc/command-handle)
