Consuming messages
Pull the bytes from your broker, decode them, gate with accepts (a TypeScript
type guard), then switch on the URN — regardless of which language produced the
message. trace_id carries the cross-service correlation id.
import { EnvelopeCodec } from "@babelqueue/core";
// body pulled from your broker (Redis BLPOP, an AMQP message, …)
const incoming = EnvelopeCodec.decode(body);
if (EnvelopeCodec.accepts(incoming)) {
// `incoming` is now narrowed to a fully-typed Envelope
switch (EnvelopeCodec.urn(incoming)) {
case "urn:babel:orders:created":
console.log(`[${incoming.trace_id}] order received`, incoming.data);
break;
}
}
That’s the whole loop: any producer, any consumer, one schema.