Command Timeout
Handle KubeMQ command execution timeouts in Node.js, detecting when no handler responds in time and failing the request cleanly.
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
- KubeMQ server running on
localhost:50000 - Node.js SDK installed (
npm install kubemq-js)
Code
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
timeoutInSeconds: 1sets 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. KubeMQTimeoutErroris 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 errin the else branch re-raises unexpected errors (connection failures, serialization errors) so they are not silently swallowed.
Related
Was this page helpful?