Producing messages with the Go SDK

Last updated:

Build the canonical envelope with Make, encode it to JSON with Encode, then publish the bytes with your own broker client.

import babelqueue "github.com/babelqueue/babelqueue-go"

env, err := babelqueue.Make(
    "urn:babel:orders:created",
    map[string]any{"order_id": 1042, "amount": 99.90},
    babelqueue.WithQueue("orders"),
)
if err != nil {
    panic(err)
}

body, _ := env.Encode() // []byte of compact UTF-8 JSON
// redisClient.RPush(ctx, "queues:orders", body)
//   /  ch.PublishWithContext(ctx, "", "orders", false, false, amqp.Publishing{Body: body})

Encode emits the canonical schema_version: 1 envelope, with meta.lang set to "go":

{
  "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": "go", "schema_version": 1, "created_at": 1749132727000 },
  "attempts": 0
}

Extension (meta) keys

Attach your own meta keys with the WithMeta option:

env, err := babelqueue.Make(
    "urn:acme:orders:created",
    map[string]any{"order_id": 1042},
    babelqueue.WithQueue("orders"),
    babelqueue.WithMeta("ext", map[string]any{"acme": map[string]any{"tier": "gold"}}),
)

Make returns an error wrapping ErrReservedMetaKey for a core key (id, queue, lang, schema_version, created_at), ErrForbiddenKey for ts, max_retries, attempts or source, ErrDuplicateMetaKey for a key supplied twice, and ErrEmptyMetaKey for "". Encode fails with ErrReservedMetaKey or ErrForbiddenKey if such a key reaches the envelope another way, so it is never put on the wire.

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.