Why broadcasting channel public not working? Laravel

I use laravel 5.3

I make routes/channels.php like this :

<?php
Broadcast::channel('messages', function() {
    return true;
});

If I input the data cart and click submit, it will run this :
this.$http.post(window.BaseUrl + '/guest/add-notification', {cart_data: JSON.stringify(data)});

It will call function on the controller

The function like this :

public function addNotification(Request $request){
    $input = $request->only('cart_data');
    $data = json_decode($input['cart_data'], true);
    event(new CartNotificationEvent($data));
}

Then it will call event

The event like this :

<?php
namespace AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateQueueSerializesModels;
use IlluminateBroadcastingPrivateChannel;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateContractsBroadcastingShouldBroadcast;
class CartNotificationEvent
{
    use InteractsWithSockets, SerializesModels;
    public $data;
    public function __construct($data)
    {
        $this->data = $data;
    }
    public function broadcastWith()
    {
        return [
            'message' => $this->data,
        ];
    }
    public function broadcastAs()
    {
        return 'newMessage';
    }
    public function broadcastOn()
    {
        return new Channel('messages');
    }
}

On the client, I do like this :
Echo.channel('messages')
.listen('.newMessage', (message) => {
    console.log('test')
    console.log(message);
});

When all the code is executed, I check on the console, the console.log not display

Why is it not working?

If I see the whole code that I make, it seems the process is correct

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

class CartNotificationEvent implements ShouldBroadcast is missing.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x