Producing messages

There are two ways to publish a message. Both emit the identical canonical envelope on the wire.

The interface (primary)

Implement ShouldQueuePolyglot on a job class. getBabelUrn() returns the URN and toPayload() returns the data body. Dispatch it with Laravel’s dispatch() helper:

use BabelQueue\Contracts\ShouldQueuePolyglot;

final class OrderCreated implements ShouldQueuePolyglot
{
    public function __construct(
        private int $orderId,
        private float $amount,
    ) {}

    public function getBabelUrn(): string
    {
        return 'urn:babel:orders:created';
    }

    public function toPayload(): array
    {
        return [
            'order_id' => $this->orderId,
            'amount'   => $this->amount,
        ];
    }
}

dispatch(new OrderCreated(1042, 99.90));

The facade (sugar)

For one-off publishes, the BabelQueue facade takes the URN and payload directly and returns the message id:

use BabelQueue\Facades\BabelQueue;

$id = BabelQueue::publish('urn:babel:orders:created', [
    'order_id' => 1042,
    'amount'   => 99.90,
]);

The emitted envelope

Either approach produces the canonical schema_version: 1 envelope:

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

Next: Consuming messages.