Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

php - Laravel Broadcasting: Notification vs Event

I read laravel documentations about Events and Notifications, it seems we can fire an event and from that event (using ShouldBroadcast interface) broadcast it to laravel echo which i understand, in the other hand we can use Notifications viaBroadcast to do the same, so what's the difference?

question from:https://stackoverflow.com/questions/39314809/laravel-broadcasting-notification-vs-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What the provided answer lacks imo is that they are in most cases used both instead of 1 or the other, which seems to be the tone of the provided answer/question.

An event is something significant in your application. Let's assume your application is a Webshop.

A significant action in your webshop can be Product Purchased . When a product is purchased you need to do a lot of different steps. Putting this all inside a controller and potentially in several different places can get very messy and not clear.

So a good approach would be to use a Event called ProductPurchased . This event can have Listeners , those listeners are in this case all the steps you need to preform when a user purchases a product.

e.g.: ProductPurchased (event)

  • BillClient (eventlistener)
  • GenerateInvoice (eventlistener)
  • notifyClient (eventlistener)
  • ...

Let's say we want to notify our client with a text-message and an email when they purchased a product.

So on the notifyClient event-listener we can create a Notification . This notification is responsible for sending a message to the client. This can be a SMS/Slack-message/Email/...

And like you mentioned both Events and Notifications can be put on the Queue or can be broadcasted.

You choose notifications when you want to send something to different channels. Mail/SMS/Slack.. If you only need broadcasting you can just use ShouldBroadcast. Just like when you only want to send a e-mail use Mail:: without the need for a notification.

Notifications are a nice way to group the same 'message' to different destinations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...