Codementor Events

Connect Laravel crons/commands with front end

Published Dec 03, 2018
Connect Laravel crons/commands with front end

Laravel is a top ranked PHP framework since last couple of years, and there are a few solid reasons why it is but we are not discussing that in this post.

One thing I like the most about laravel framework is the ease of writing cron jobs (AKA Commands) and maintinig them. But a lot of time I've found myself into situations where I wanted to display what the cron is currently doing, on the frontend. And I believe almost all the developers want to do it.

Real-time notifications are vital to any application (web or versatile) and Pusher is a standout amongst the most well known administrations which encourages you convey continuous notifications to your applications. I have utilized Pusher on a few activities and it was a pleasure to actualize. So given us a chance to perceive how we can execute constant notices in this laravel pusher instructional exercise.

Before we start you should first obtain an app_id, key and secret for free Pusher.com

Install the pusher package in your laravel application

composer require pusher/pusher-php-server

I believe at this point you already have a cron/command class with some code in it. In case you don't and following this article for learning purposes only then create a cron class by running the following command:

php artisan make:command ExampleCommand

Lets just assume that your command doesn't do much and just pulls all the active users from users table and iterates over them sending the name to front end, put the following command in your handle() function:

public function handle() {
  $users = App\User::where('is_active', 1)->get();
    
    foreach($users as $user) {
    	//You will get cluster name from pusher.com replace it below
        $options = ['cluster' => 'mt1', 'encrypted' => true];
 
       //Replace your key, app_id and secret in the following lines 
        $pusher = new Pusher(
            'key',
            'secret',
            'app_id',
            $options
        );
        
        $message= "The current user name is $user->name";
        
        //Send a message to users channel with an event name of users-list
        $pusher->trigger('users', 'users-list', $message);  
    }
}

Now you should have a route which accepts GET request and returns a view, at the bottom of that view you should put the following code :

    <!-- Incldue Pusher Js -->
    <script src="https://js.pusher.com/4.2/pusher.min.js"></script>
    <script>
    //Remember to replace key and cluster with the credentials that you have got from pusher.
    var pusher = new Pusher('key', {
      cluster: 'mt1',
      encrypted: true
    });
 
    //In case you have decided to use a different channel and event name then change it here to match with the one that you have used
    var channel = pusher.subscribe('users');
    channel.bind('users-list', function(message) {
    // for demonstration purpose I am just alerting the message passed by the cron/command but in a real life/production application you should never do that
        alert(message);
    });
 
    </script>

And thats it run your command from terminal and you should start seeing the notifications.

Image source: scotch.io

Discover and read more posts from Shahrukh Khan
get started
post comments1Reply
Abdur-Rahmaan Janhangeer
6 years ago

i believe that clear code samples by themselves go a long way teaching about the subject. thanks for article.