mercredi 19 mai 2021

Why the triggered pusher data is not updated in the view (blade) file in the browser?

I have the blade file with 3 div's

welcome.blade.php

 <div id="id1">0</div>
 <div id="id2">0</div>
 <div id="id3">0</div>
 <script>
    //pusher
    var pusher = new Pusher('xxxxxxxxxxxx', {
        cluster: 'eu',
        encrypted: true
      });
   // Subscribe to the channel we specified in our Laravel Event
      var channel = pusher.subscribe('my-channel');
   // Binding a function to a Event
     channel.bind('App\\Events\\UpdatedTime', function(data){
      target = data.id;
      console.log(data);
      value = data.value;
      var div1 = document.getElementById(target);
      div1.innerHTML =value;
     });
</script>

The 0 in the div should be replaced by the value which is triggered by the event in the pusher.

I could get the event triggered in the pusher like the below in the debug console, when I pass the Id in the URL.(http://project.test/id1) enter image description here

In my controller:

 public function getTime($divId)
    {

     // pusher
        $options = array(
            'cluster' => 'ap3',
            'useTLS' => true
        );

        $pusher = new Pusher(
            env('PUSHER_APP_KEY'),
            env('PUSHER_APP_SECRET'),
            env('PUSHER_APP_ID'),
            $options
        );
        //var_dump($pusher);
        $pusher->trigger('my-channel', 'UpdatedTime', ['id' => $divId, 'value' => 1511);
        return view('welcome');


    }

Events File

  public $id, $value;
    public function __construct($id, $value)
    {
       $this->id = $id;
       $this->value = $value;
    }
    public function broadcastOn()
    {
        return new Channel('my-channel');
    }

Routes/web.php

Route::get('/getTime/{id}', 'App\Http\Controllers\TimeController@getTime');

After passing the URL in the browser, http://project.test/id2

I could only see

0
0  
0

in the browser, Actually it should be in the browser.

0
1511
0

How can I get the triggered event value in the browser? Could someone please help?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire