tlaverdure / laravel-echo-server

Socket.io server for Laravel Echo
MIT License
2.65k stars 510 forks source link

Never gets into Broadcast::channel... #494

Closed patternknife closed 4 years ago

patternknife commented 4 years ago

Hi, I am using redis + laravel-echo. Socket works well in case that server-side data is sent to client browsers, but the thing is... the opposite direction does not work at all with an error message like "Unable to join channel. Member data for presence channel missing".

// routes/channels.php
 Broadcast::channel('chatgo', function ($user) {

   // Can't get here T.T....

    var_dump('xxx');
    $aaa = 1;
    return 1;
});

This kind of thing never works...

        Echo.join('chatgo').here((users) => {
            //
            alert('cccc');
        })
            .joining((user) => {
                console.log(user);
            })
            .leaving((user) => {
                console.log(user);
            });;

Should I call AJAX every time a client leaves the channel or types and enters a message on browser? That way, I guess I could solve the issue. I guess "this.socket.emit.." on "laravel-echo" is not an issue.

patternknife commented 4 years ago

After going over the source code, I found out it parsing a parameter "channel_data".

// /auth/broadcasting (your auth api)
    public function broadcast(Request $request)
    {
        $channel_name = $request['channel_name'];

        // your codes for additional checks
       ....
       // key : 'channel_data', value : User object  e.g) \Auth::user()
        return response()->json(['channel_data' => $request->user()], 200);
}

This also prevents an error like this.

    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:31560) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3).

However, there are some additional problems with discrepancies among Laravel, Laravel-echo and this Laravel-echo-server parameters in the user object. This is a temporary solution for me.

    public function broadcast(Request $request)
    {

        $channel_name = $request['channel_name'];
        $user = $request->user();  // or \Auth::user() 

        // your codes for additional checks with $channel_name & $user
       ....
        $member = [];
        $member['user_info'] = $user->toArray();
        $member['user_id'] = $member['user_info']['id'];

        return response()->json(['channel_data' => $member], 200);

    }