0

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

Wondering Coder
  • 1,652
  • 9
  • 31
  • 51
  • Create a php page that executes the desired toastr function and then call that page from cronjob? (I know nothing of toastr) – Epodax Oct 01 '15 at 10:57

1 Answers1

1

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).

Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
  • thank you very much for the suggestion, really appreciate it. I like the idea of the javascript but I'm concern with the load. Hmm, i guess I will sit this aside for now. thank you again.. – Wondering Coder Oct 02 '15 at 01:46
  • have you used the Events of laravel5.1. http://laravel.com/docs/5.1/events. Do you think I can use this? But i dont want to setup a broadcaster... – Wondering Coder Oct 02 '15 at 01:50
  • the Events/Listeners would normally get fired on page reload which isn't quite what you want, if you read the docs of the Events you'll see this: http://laravel.com/docs/5.1/events#broadcasting-events so again WebSockets, there is no way to update the application without either open persistent connection to the server over a socket or poll regularly your server for changes. – Alex Rashkov Oct 02 '15 at 08:46