How graffish-labs/symfony-nats-transport works
Project type and purpose
• This is a Symfony Messenger transport bundle ("graffish-labs/symfony-nats-transport") that plugs into a Symfony application.
• Its purpose is to let the Symfony Messenger component publish and consume messages via a NATS server.
File structure
• Top level: composer files, config folder, src folder.
• config/services.yaml: service definitions for the bundle.
• src/DependencyInjection: bundle extension and configuration classes (loads user config, registers services).
• src/GraffishLabsSymfonyNatsTransportBundle.php: the Bundle class that hooks into Symfony’s DI.
• src/Transport: NatsTransportFactory (creates the transport), NatsTransport (implements sending/receiving via NATS), and a config bean.
README
• Confirms dependencies: PHP 8.1+, Symfony 6+, Messenger 7.2+, NATS server.
• Lists features: reconnection logic, serialization (using Symfony Serializer), support for retries/failures.
a. Main components
• Symfony Application (FrameworkBundle, Messenger)
• GraffishLabsSymfonyNatsTransportBundle (Bundle class, DI Extension, Configuration)
• NatsTransportFactory
• NatsTransport
• Symfony Serializer component
• NATS Server (external)
• MessageBus (async transport layer)
• User-defined message handlers (inside the Symfony app)
b. Relationships & interactions
1. At boot: Bundle class registers DependencyInjection Extension → loads config/services.yaml → registers NatsTransportFactory and NatsTransport as messenger transports.
2. When you configure messenger.yaml with “nats://…”: Messenger asks the TransportFactory to create a NatsTransport instance.
3. NatsTransport uses the Symfony Serializer to turn your Message objects into bytes.
4. Publish flow: MessageBus → MessengerTransport (NatsTransport) → NATS Server (Pub).
5. Subscribe flow: NatsTransport opens subscription to NATS Server → receives messages → uses Serializer to decode → dispatches to MessageBus → handlers.
6. Retry/Failure: on exception, messenger retry/failure transports engage per Symfony Messenger policies.
c. Architectural patterns & design principles
• Dependency Injection (bundle extension registers services)
• Factory pattern (NatsTransportFactory)
• Adapter pattern (NatsTransport adapts NATS client to Symfony Messenger TransportInterface)
• Plugin/Bundle architecture (integrates into Symfony via Bundle class)
d. Key technologies & libraries
• Symfony Framework & Messenger component
• PHP 8.1 types and attributes
• Symfony Serializer (for message payloads)
• NATS PHP client (under the hood in NatsTransport)
• YAML configuration
Elements
• Blue rectangles for core Symfony components
• Green rectangles for bundle/DI classes
• Orange circle for external NATS Server
• Clear callouts for configuration: service definitions in config/services.yaml and messenger.yaml.
• Layer 1 (Configuration & Boot): Bundle class & DependencyInjection → registers factory & transport.
• Layer 2 (Factory & DI): NatsTransportFactory reads DSN & options → instantiates NatsTransport + Serializer.
• Layer 3 (Transport): NatsTransport implements TransportInterface; manages connection to NATS, reconnection logic, subscribes/publishes.
• Layer 4 (External): NATS Server as message broker.
• Layer 5 (Runtime Flow): MessageBus → Transport → NATS → Transport → MessageBus → Handlers