Symfony messenger with JSON messages

Symfony messenger with JSON messages

When you start a new project with the Symfony messenger component, the messages will be serialized through the PHP serialization. This has the advantages that you don't need a constructor with all properties and no public properties. On the other hand when you look into the database to see the current messages, it's very difficult to read.

I'm working with Psalm and the @psalm-immutable annotation for the class. So there is no problem with public properties and as they are immutable, I also need to provide a full constructor. Therefore the advantages of this default behaviour aren't relevant for me. Only the disadvantages.

The Symfony serializer component started it's life with a JSON serializer and there is an easy way to get it back. Simply configure the still available messenger.transport.symfony_serializer serializer in your messenger.yaml:

framework:
  messenger:
    transports:
      async:
        ...
        serializer: messenger.transport.symfony_serializer

Make sure that you don't have any messages in your table when you switch to this serializer as all existing with another serializer won't be readable.