JetStream persistence for Symfony Messenger
(php, symfony, nats) 1 min read by Bartosz Pachołek
Sending and consuming Symfony Messenger messages through NATS with JetStream durability, using symfony-nats-messenger.
Symfony Messenger is a clean way to move work out of the request. With symfony-nats-messenger the transport is NATS JetStream, so messages are stored, survive restarts and are acknowledged when handled.
Set it up
composer require idct/symfony-nats-messenger
NATS needs JetStream enabled (nats-server -js). Then point a transport at a stream and a topic:
# config/packages/messenger.yaml
framework:
messenger:
transports:
nats_transport:
dsn: 'nats-jetstream://localhost:4222/my-stream/my-topic'
options:
consumer: 'my-consumer'
batching: 5
max_batch_timeout: 1.0
routing:
'App\Message\MyAsyncMessage': nats_transport
Dispatch messages as usual and run bin/console messenger:consume nats_transport.
Pick a consumer strategy
- One shared consumer,
batching: 1. Several workers cooperate on the same consumer and NATS balances the load; each fetches and acknowledges one message at a time, and every message is processed once. - One consumer per worker, any batching. Every consumer gets every message: a fan-out for broadcasting events to independent processors, with larger batches for throughput.
The README explains both, along with batch and connection timeouts, retention policies and replication.
The transport runs on Symfony 7.2 and 8 with PHP 8.2+, and is MIT licensed.