Bossino's avatar

Laravel Pusher

I'm currently learning pusher and applying it in my queuing management project. I almost done applying it in my project as I do this:

In my blade view file in section

var channel = pusher.subscribe('my-channel');
    channel.bind('my-event', function(data) {
        alert(JSON.stringify(data));
      
 });

which I test that my pusher is working. Now my question is how do I refresh a specific div after the data was sent. Like this

var channel = pusher.subscribe('my-channel');
    channel.bind('my-event', function(data) {
        //refresh specific div here after the data was sent     
 });

I will show also the controller

public function store(Request $request)
{
      $call = Call::create([
                'letter' => $letter->letter,
                'number' => $number->start,
                'called' => 'NO',
                'trans_id' => $request->input('trans_id'),
                'guest_name' => $request->input('name'),
                'amount' => $request->input('amount'),
                'num_calls' => 0
           ]);

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

            $pusher = new Pusher(
                env('PUSHER_APP_KEY'),
                env('PUSHER_APP_SECRET'),
                env('PUSHER_APP_ID'),
                $options
            );

            $data = ['letter' => $call->letter, 'number' => $call->number, 'id' => $call->id]; 
            $pusher->trigger('my-channel', 'my-event', $data);
0 likes
7 replies
experimentor's avatar

Assuming that the specific div is identified with the id coming in the data. So, if data.id = 3, assuming that the specific div has id="call_3".

If you are using jQuery this code should work:

var channel = pusher.subscribe('my-channel');
        channel.bind('my-event', function(data) {
            const html = `<p>Letter: ${data.letter}</p><p>Number: ${data.Number}</p>`;
            const specific_div = $(`#call_${data.id}`);
            // Empty the div
            specific_div.empty();
            // Populate new html
            specific_div.append(html);

    });
Bossino's avatar

Okay sir. But my div id is not with the data something like this <div id="new_call>. Is it okay or is it really need to have an id num on it? and also can I do this also

const html = `<input type="hidden" class="form-control" id="call_id" name="call_id" value="${data.id}" readonly><span class="letter"> ${data.letter}</span>-<p>Number: ${data.Number}</p>`;

so that I can change also the value in the input?

experimentor's avatar

If there is only one div in the page like you said <div id="new_call>, then we can define the specific div as:

const specific_div = $(`#new_call`);

If you have many divs showing all the calls and you need to modify a specific one, you will need to use the data.id in the identity of every div. That will allow for selecting that div when we get data from pusher. But, please note: the above method of selecting and updating specific div will work only if you have Jquery. Otherwise, you will have to use pure javascript, or any framework which may already be installed.

About the second part of your question: Yes. You can use any sort of html and append it to specific div.

I suggest you go through javascript DOM management / JQuery. Here is a w3schools link: https://www.w3schools.com/jquery/default.asp

Bossino's avatar

The specific div reload did not work for me. I think I have an error in my code here

call.blade.php

<div class="col-md-12" id="new_call">
            @if($callid === null)
        
            @else
                <form id="form-data" class="form-horizontal" method="POST" action="updatecall">
                    {{ csrf_field() }}
                    <div class="table-responsive">
                        <table class="table table-bordered" id="example" width="100%" cellspacing="0">
                            <tr>
                                <input type="hidden" class="form-control" id="call_id" name="call_id" value="{{ $callid->id }}" readonly>
                                <td><label>Probably Next Call</td>
                                <td><span id="letter">{{ $callid->letter }}</span>-<span id="number">{{ $callid->number }}</span></td>
                                <td>
                                    <button type="submit" class="btn btn-success btn-fill pull-right" id="update" float:right>
                                        CALL NEXT
                                    </button>
                                </td>
                            </tr>
                        </table>
                    </div>
                </form>
            @endif
</div> 

I just paste my specific div which is set to refresh

Pusher Javascript code also in call.blade.php

var channel = pusher.subscribe('my-channel');
    channel.bind('my-event', function(data) {
        const html = `<input type="hidden" class="form-control" id="call_id" name="call_id" value="${data.id}" readonly><span class="letter"> ${data.letter}</span>-<span class="number">Number: ${data.Number}</span>`;
        const specific_div = $('#new_call');
        // Empty the div
        specific_div.empty();
        // Populate new html
        specific_div.append(html);
});
Bossino's avatar

UPDATE: I got it now. My problem is I cannot click the call next button after a pusher update.

experimentor's avatar
Level 25

Now that I see the full HTML code, I can help you better.

When we call specific_div.empty(), entire form will be destroyed. After that we are only generating the hidden input and a couple of spans. So the form will not work.

In your code, I see that we have elements with id and classes. Please add id tag for letter and number. id="letter" id="number"

Modify code in the bind function to this:

$("#call_id").val(data.id);
$("#letter").html(data.letter);
$("#number").html(`Number: ${data.Number}`);

Please spend some time on the essentials of Javascript DOM management and JQuery. It will help you a lot.

Bossino's avatar

Thank you Sir @vishy . I will spend some time on Javascript DOM management and JQuery. Thanks a lot.

Please or to participate in this conversation.