简体   繁体   中英

Laravel 5.1: Difference between Event and Mail:queue?

I have registration form where I want to send an email to users after they register successfully. I want to run email in background so that page's response time is a bit better.

For email in background, I saw that we can use:

  • Event with ShouldQueue
  • mail::queue() method.

My question is that in the context of queuing email , what the difference between above two methods ? Does one behave different from other when sending email via queue ?

When the application is running you may have events (such as UserHasLoggedIn) that are picked up by listeners that may carry out actions like AddEntryToAuditLog. The important thing about these is that they start and end during the call to Laravel, and there's nothing to be done afterwards. http://laravel.com/docs/5.0/events

A job is something that may need to be done after Laravel has finished executing because it needs to be delayed (a follow-up email a day later) or takes a long time (eg preparing a zip backup of hosted files). These jobs are put into a queue using Laravel's provider or an external queue provider. The queue takes care of monitoring the pending jobs and running them according to the order they arrived and any scheduling requirements. http://laravel.com/docs/5.0/queues

There is some cross-over. An event could trigger a listener and register a job. Eg A userRequestedBackup event fires - a listener picks up up the event to send a notification email, and a job is queued to zip up the files ready for sending. http://laravel.com/docs/5.0/events#queued-event-handlers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM