Search code examples
phplaravel-5.1toastr

Using toastr in a cron job


I'm using laravel toastr feature (link here). I just want to ask if it's possible or is there any way to have the toastr functionality inside a cron job then throw the toastr result in the web page?

Also I dont want to use any web socket. I'm just asking if anyone has a suggestion.

Thanks


Solution

  • OK if I understand correctly what you want to do is an async message to the user one way to do it is use WebSocket and add a JavaScript that listens to that and if there is any incoming data show the toastr. Problem is you don't want WebSockets. The other option is hsow the toastr message once the user load the page but this won't be exactly what you want as that will require page reload to get the data from the server though I guess that's as close as you can get. You can have following code in the main template file:

    @if (Session::has('flash_notification.message'))
    
        <script>
            $(document).ready(function() {
    
            toastr.{{ Session::get('flash_notification.level') }}
            ('{{ Session::get('flash_notification.message') }}');
    
            });
        </script>
    
    @endif
    

    and you need another script or Laravel command that gets executed regularly from the CRONJOB and that should write to DB or file. Once the data has been written you can read it in the Contrller or the Model and append it in the Session, you can use a timestamp so you don't print duplicate toastrs.

    Alternativelly you can have a JavaScript in the page that will poll an endpoint every-now-and-then and if there's been update it will show the notification but that will add extra load to your webserver (Apache).