There was a need to notify the user about events in the system in real time.

Used as the basis of laravel 5.3. But I don’t know how to organize it properly and what is needed for that. Tell me how to build a similar system. Maybe I am looking in the wrong direction, but all I have so far found is Events in laravel

2 types of notifications:

  1. Letters
  2. Post on the front as a window with text.
  • what events in the system? What method do you want to notify the user? - Arsen
  • @Arsen for different events in different ways. In general, for the time being there should be 2 types of 1st letter 2nd notification on the front in the form of a window with a message - Shadow33
  • @Orange_shadow 9 bucks a month for the second lesson (But I caught the point - Shadow33
  • Look for trackers for example here rutracker.cr - Orange_shadow

2 answers 2

In a nutshell, abstracted, add methods that check the information when it appears that you need to issue a notification. For example

public function newMessage() { // проверяем... //если есть новое setcookie("newMessage", $count, time() + 86400); return 'yes'; //если нет return 'no'; } 

Add a route to this method. At the front, use ajax to listen to these cookies.

 <script> + function newMsg(data, d){if(data == "yes"){ + $('.new-msg').css('display', 'block'); + }} + setInterval(function(){ + $.ajax({ + url: "/new-message", + type: "POST", + data: ({popupMsg: "go"}), + dataType: "html", + success: newMsg + }); + },300000) </script> 

Something like this...

    I did something similar, implemented as follows:

    1. Email, SMS, etc. best done through a new feature that appeared in version 5.3: Laravel Notifications

    If in the future you want more than just an email notification, check out the site - http://laravel-notification-channels.com/ there are many channels for notifications.

    1. Notifications in the browser are best implemented through a new feature of version 5.3: Event Broadcasting

    Let's start with the fact that we will choose a driver for Event Broadcasting, I will show Pusher as an example.

    To begin, make changes to the .env file:

     BROADCAST_DRIVER=pusher PUSHER_APP_ID=#данные с панели Pusher PUSHER_KEY=#данные с панели Pusher PUSHER_SECRET=#данные с панели Pusher 

    Download the package through Composer:

     composer require pusher/pusher-php-server 

    While with Pusher everything, in the end we will return to it again.

    Create an event (examples and description are here ) for example, we will send a notification when someone writes to the user. The notification object will look something like this:

     <?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Queue\SerializesModels; class ChatMessageSend implements ShouldBroadcast { use SerializesModels; /** * @var User */ public $user; /** * @var array */ public $data; /** * Create a new event instance. * @param array $data */ public function __construct(User $user, array $data) { $this->user = $user; $this->data = $data; } /** * Get the channels the event should broadcast on. * * @return Channel|array */ public function broadcastOn() { return ['chat-channel']; } public function broadcastAs() { return 'message-received'; } } 

    The broadcastOn and broadcastAs methods are needed for Event Broadcasting, you also need to create a Listener for this event in order to send Email and other messages in it.

    Now you need to create Mail Notification in which we specify a letter template, an example is available by reference, let's call it for example MessageSend.

    Next, you need to create a Listener that will trigger this notification:

     <?php namespace App\Listeners; use App\Events\ChatMessageSend; use App\Notifications\MessageSend; class SendChatMessageNotification { /** * Create the event listener. */ public function __construct() { // } /** * Handle the event. * * @param ChatMessageSend $event * @return void */ public function handle(ChatMessageSend $event) { $event->user->notify(new MessageSend($event->data)); } } 

    Now the EventServiceProvider file EventServiceProvider link the event and the listener

     protected $listen = [ 'App\Events\ChatMessageSend' => [ 'App\Listeners\SendChatMessageNotification', ], ]; 

    Now everything is ready for sending notifications. To send a notification, you need to trigger an event:

     event(new ChatMessageSend($user, $data)); 

    The e-mail went away, and went to the Pusher service, but now we need to display it in a browser, there are examples for different languages ​​on the Pusher website, I’ll output for JS:

     <!DOCTYPE html> <head> <title>Pusher Test</title> <script src="https://js.pusher.com/4.0/pusher.min.js"></script> <script> // Enable pusher logging - don't include this in production Pusher.logToConsole = true; var pusher = new Pusher('key', { cluster: 'eu', encrypted: true }); var channel = pusher.subscribe('chat-channel'); channel.bind('message-received', function(data) { alert(data.data); }); </script> </head> 

    Instead of alert, write your JS for browser notifications.

    PS The code should be working, but could somewhere miss something