Laravel 7 event pusher broadcasting

Viewed 17

I want to push the counter in the chat section like below:

    public function userChat(Request $request)
    {
        if (Auth::guard('admin')->check()) {
            $user = Auth::guard('admin')->user()->name;
        }
        if (Auth::guard('web')->check()) {
            $user = Auth::user()->name;
            $userName = Auth::user()->name;
        }

        $user_id = $request->get('user_id');
        $purpose = $request->get('purpose');

        $owner = '';
        if ($user == $user_id)
            $owner = 'me';
        $owner = 'you';

        // dd($adminName);
        if ($purpose == 'find-user') {
            $msg = Chat::where('from', $user)->where('to', $user_id)
                ->orWhere('from', $user_id)->Where('to', $user)
                ->get();
            // dd($msg);
            return response()->json($msg);
        } elseif ($purpose == 'admin-user') {
            $val = $request->get('val');
            $chat = Chat::create([
                'message' => $val,
                'from' => $user,
                'to' => $user_id
            ]);
            $data = 'xxx';
            event(new chatNotify($data));
            return response()->json(['user' => $user, 'msg' => $val]);
        }
    }

in my evet channel file:

    public $name;
    public function __construct($data)
    {
        $this->name = $data;
    }
    public function broadcastOn()
    {
        //return new Channel('chatNotify');
        return ['chatNotify'];
    }

in my javascript file in the app:

var notificationsWrapper = $('#user-chat');
var notificationsCountElem = notificationsWrapper.find('span[data-count]');
var notificationsCount = parseInt(notificationsCountElem.data('count'));

// Subscribe to the channel we specified in our Laravel Event
var channel = pusher.subscribe('chatNotify');
// Bind a function to a Event (the full Laravel class)
channel.bind('App\\Events\\chatNotify', function () {
    notificationsCount += 1;
    notificationsCountElem.attr('data-count', notificationsCount);

I have an error connection, no pushing was happen, can somebody help me, please am using ajax in chat section connection

0 Answers
Related