Producing messages with the Node.js SDK
Last updated:
Build the canonical envelope with EnvelopeCodec.make, encode it to JSON with
EnvelopeCodec.encode, then publish the bytes with your own broker client.
import { EnvelopeCodec } from "@babelqueue/core";
const env = EnvelopeCodec.make(
"urn:babel:orders:created",
{ order_id: 1042, amount: 99.90 },
{ queue: "orders" },
);
const body = EnvelopeCodec.encode(env); // compact UTF-8 JSON string
// await redis.rpush("queues:orders", body);
// / channel.sendToQueue("orders", Buffer.from(body));
CommonJS works too: const { EnvelopeCodec } = require("@babelqueue/core").
encode produces the canonical schema_version: 1 envelope, with meta.lang
set to "node":
{
"job": "urn:babel:orders:created",
"trace_id": "7b3f9c2a-e41d-4f88-9b2a-1c0d5e6f7a8b",
"data": { "order_id": 1042, "amount": 99.90 },
"meta": { "id": "f1e2d3c4-b5a6-4789-90ab-cdef01234567", "queue": "orders", "lang": "node", "schema_version": 1, "created_at": 1749132727000 },
"attempts": 0
}
Extension (meta) keys
Pass your own meta keys through the meta option of EnvelopeCodec.make:
const env = EnvelopeCodec.make(
"urn:acme:orders:created",
{ order_id: 1042 },
{ queue: "orders", meta: { ext: { acme: { tier: "gold" } } } },
);
make throws InvalidEnvelopeKeyError when meta collides with a core key (id, queue, lang,
schema_version, created_at) or a forbidden key (meta.max_retries, meta.attempts,
meta.source, meta.ts); its pointer holds the RFC 6901 pointer of the key. encode throws the
same error for any non-canonical key still on the envelope (top-level timestamp or urn, or a
forbidden meta key).
Keep vendor data under a single meta.ext.<vendor> block and use snake_case keys
(ADR-0031). The entries are written next to the core meta keys and survive every retry,
dead-letter and redrive hop. See Extension keys in the
envelope spec.
Next: Consuming messages.