Messenger! Queue work for Later

5:00:09

What you'll be learning

Symfony's Messenger component gives you a beautiful system for designing your code around "message" objects and decoupled "handlers" that do the work!

... wait... that's not right, it's really...

Symfony's Messenger component gives you the power to execute code asynchronously via queues and workers!

Actually, it's both! Messenger is one of the newest parts of Symfony and we absolutely ❤️ it. In this tutorial, we'll create an important application that asynchronously adds our favorite cat (Ponka) to all of your favorite photos. Along the way, we'll learn to:

  • Create a messages classes
  • Add & configure "handler" classes that do the work
  • Dispatch messages through the bus
  • Configure "transports" for async messages
  • Route messages to transports
  • Execute "worker" processes that "consume" queued messages
  • Things fail!? Retry message and set up the "failure transport"
  • Middleware
  • Add & understand message stamps
  • Prioritized transports
  • A little CQRS: a Query bus
  • Testing & Dev: handling messages "sync"
  • Deployment and supervisor
  • RabbitMQ & AMQP

Let's get to work!


Your Guides
Ryan Weaver

Buy Access
Login or register to track your progress!
This course is still being released!
Check back later for more chapters.

Questions? Conversation?

  • 2019-07-30 Diego Aguiar

    Hey ElGovanni

    Yes, that's a good use case. Writing a big file is another good example

    Cheers!

  • 2019-07-30 ElGovanni
    it's useful for any tasks that are heavy


    Like sending e-mail by symfony mailer?

  • 2019-07-24 weaverryan

    Hey @ElGovanni!

    I think, even if you don’t have any heavy work, Messenger enables this “command bus design pattern”. If you love that pattern, it’s awesome. If you don’t care so much, then yea, there’s much less use for it. So, it’s useful for any tasks that are heavy OR if you just want to leverage the command bus pattern.

    Cheers!

  • 2019-07-24 ElGovanni

    Cool feature but after 14 episodes I don't have in mind any use case for messenger.

  • 2019-07-24 Victor Bocharsky

    Hey skyCatalysm,

    Thank you for your interest in this screencast, your vote is counted. We do plan to record it some day, but unfortunately, we don't have any estimations yet - too many good stuff we would like to work on before it.

    Thank you for your patience!

    Cheers!

  • 2019-07-23 Kegan VanSickle

    Awesome series and perfect timing for me as well! I have a big project coming up that will utilize the Messenger component.

  • 2019-07-19 skyCatalysm

    Hi! will symfony cast ever tackle about mercure bundle? and when?

  • 2019-07-16 Skylar Scotlynn Gutman

    I get what your saying and it makes sense. I was going to swallow the whole dragon and test from the message dispatch to the results of the handlers work in one test.

    Thank again for your time. It's always appreciated.

  • 2019-07-16 weaverryan

    Hey Skylar Scotlynn Gutman!

    I don't think that specific topic will make it into this tutorial, so I'll give you some tips here :). The main reason is that (delightfully) there is nothing special about unit testing a handler. If you think about it, a handler is no different than any other service you might build in your app - it's a service, it uses dependency injection and it has a public method that "does" something. The only difference is that the method is called __invoke, which means that when you're calling it in your test, instead of $myService->myMethod('fooArg') it will be $myService('fooArg') (you execute the object like a function).

    The one other interesting thing about a handler is that they (unless you're using messenger as a query bus... but, different topic) never return anything. That's not a strict requirement - messenger doesn't care if you do - but you typically don't, because Messenger doesn't care about your return value (and neither should you when you dispatch to Messenger). This means that your unit tests will typically be testing what a handler function *does* instead of what it returns - e.g. you might test that it calls flush() on the entity manager. If you find that there are some other things internally that you'd like to test but are having trouble (for example, you want to test that an entity was created and with the correct data... before being saved), it's probably a sign that you should refactor that small piece you want to test into its own service, test that, then autowire the new service into your handler.

    Let me know if this helps! If i missed the heart of your question, definitely tell me ;).

    Cheers!

  • 2019-07-16 Skylar Scotlynn Gutman

    Hello guys,

    Thank you for this tutorial.

    I was wondering if you could help me write a unit test for the message handler. How would you go about it? May be you could add a chapter on the topic.

  • 2019-06-18 Huy Nguyen

    wow, I would love to watch this!!!!

  • 2019-06-15 Amin Behravesh

    I've been waiting for this course for a long time.
    Great job.