# symfony/messenger architecture

> Architecture diagram of the GitHub repository symfony/messenger, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/symfony/messenger
- Repository: https://github.com/symfony/messenger

## Overview

##### Main Components

a) Core Message Bus System:
- MessageBus (Central component)
- Envelope (Message wrapper)
- Middleware Stack
- Handlers System

b) Transport Layer:
- Transport Interfaces
- Senders
- Receivers
- Serialization System

c) Worker System:
- Worker Component
- Event System
- Command System

**┌─────────────────────────────────────────────────────────┐**
- Client Application                    │
- ┘
- ↓
**┌─────────────────────────────────────────────────────────┐**
- Message Bus Layer                    │
- ┌─────────────┐    ┌─────────────┐    ┌─────────────┐ │
- Envelope  │ -> │  Middleware │ -> │  Handlers   │ │
- ┘    └─────────────┘    └─────────────┘ │
- ┘
- ↕
**┌─────────────────────────────────────────────────────────┐**
- Transport Layer                       │
- ┌─────────────┐    ┌─────────────┐    ┌─────────────┐ │
- Senders   │ <> │ Serializer  │ <> │  Receivers  │ │
- ┘    └─────────────┘    └─────────────┘ │
- ┘
- ↕
**┌─────────────────────────────────────────────────────────┐**
- Worker Layer                        │
- ┌─────────────┐    ┌─────────────┐    ┌─────────────┐ │
- Events    │ <- │   Worker    │ -> │  Commands   │ │
- ┘    └─────────────┘    └─────────────┘ │
- ┘

##### Important Details

a) Message Flow:
- How messages are wrapped in Envelopes
- Middleware processing chain
- Transport serialization/deserialization flow

b) Handler System:
- HandlerLocator
- Handler resolution process
- Batch handling capability

c) Transport System:
- Available transport types (Sync, InMemory)
- Serialization process
- Sender/receiver relationship

##### Additional Elements

- A section for Stamps (metadata attached to messages)
- Error handling and retry mechanisms
- Event dispatcher integration
- Command line tools integration

##### Specific Component Relationships

- MessageBus and Middleware
- Middleware and Handlers
- Transport and Serialization
- Worker and Event System
- Worker and Command System

##### Implementation Notes

- Annotations for important configuration points

Remember to:
- Related components together
- Notes for important architectural decisions

The key is to show how messages flow through the system, from initial dispatch through middleware processing, to eventual handling or transport to external systems, while maintaining clarity about the separation of concerns between different layers of the architecture.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client["Client Application"]
        App[Client Code]
    end

    subgraph MessageBusLayer["Message Bus Layer"]
        MB["Message Bus"]:::core
        Env["Envelope"]:::core
        Mid["Middleware Stack"]:::core
        Hand["Handler System"]:::core
        Stamp["Stamp System"]:::core
    end

    subgraph TransportLayer["Transport Layer"]
        direction TB
        TI["Transport Interface"]:::transport
        Send["Sender System"]:::transport
        Recv["Receiver System"]:::transport
        Ser["Serialization"]:::transport
        InMem["In-Memory Transport"]:::transport
        Sync["Sync Transport"]:::transport
    end

    subgraph WorkerLayer["Worker Layer"]
        Work["Worker"]:::worker
        WMeta["Worker Metadata"]:::worker
        Ev["Event System"]:::worker
        Cmd["Command System"]:::worker
    end

    subgraph Support["Support Systems"]
        Ex["Exception System"]:::support
        Retry["Retry System"]:::support
        EvList["Event Listeners"]:::support
    end

    %% Relationships
    App --> MB
    MB --> Env
    Env --> Mid
    Mid --> Hand
    Env --> Stamp
    
    MB <--> TI
    TI --> Send
    TI --> Recv
    Send <--> Ser
    Recv <--> Ser
    TI --> InMem
    TI --> Sync
    
    Work --> Ev
    Work --> Cmd
    Work --> WMeta
    
    Mid --> Ex
    Ex --> Retry
    Ev --> EvList

    %% Click Events
    click MB "https://github.com/symfony/messenger/blob/7.2/MessageBus.php"
    click Env "https://github.com/symfony/messenger/blob/7.2/Envelope.php"
    click Mid "https://github.com/symfony/messenger/tree/7.2//Middleware"
    click Hand "https://github.com/symfony/messenger/tree/7.2//Handler"
    click TI "https://github.com/symfony/messenger/blob/7.2//Transport/TransportInterface.php"
    click Send "https://github.com/symfony/messenger/tree/7.2//Transport/Sender"
    click Recv "https://github.com/symfony/messenger/tree/7.2//Transport/Receiver"
    click Ser "https://github.com/symfony/messenger/tree/7.2//Transport/Serialization"
    click InMem "https://github.com/symfony/messenger/tree/7.2//Transport/InMemory"
    click Sync "https://github.com/symfony/messenger/tree/7.2//Transport/Sync"
    click Work "https://github.com/symfony/messenger/blob/7.2/Worker.php"
    click WMeta "https://github.com/symfony/messenger/blob/7.2/WorkerMetadata.php"
    click Ev "https://github.com/symfony/messenger/tree/7.2//Event"
    click Cmd "https://github.com/symfony/messenger/tree/7.2//Command"
    click Stamp "https://github.com/symfony/messenger/tree/7.2//Stamp"
    click Ex "https://github.com/symfony/messenger/tree/7.2//Exception"
    click EvList "https://github.com/symfony/messenger/tree/7.2//EventListener"
    click Retry "https://github.com/symfony/messenger/tree/7.2//Retry"

    %% Styles
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef transport fill:#4CAF50,stroke:#2E7D32,color:white
    classDef worker fill:#FF9800,stroke:#EF6C00,color:white
    classDef support fill:#9E9E9E,stroke:#616161,color:white

    %% Legend
    subgraph Legend
        C[Core Components]:::core
        T[Transport Components]:::transport
        W[Worker Components]:::worker
        S[Support Systems]:::support
    end
```
