private channel in laravel echo with socket.io return 403 error only when i pass data with channel name?

  laravel, laravel-echo, redis, socket.io

i am using laravel 8.12 with laravel echo and redis
problem : when i add chat_id to private channel name from event, socket request return 403 error however Presence channel work correctly and there is my code:
controller:

public function store(Request $request)
    {
    
      $message = Message::create(['sender_id' => $request->sender_id,'content' => $request->message ,'message_type' => 'message','chat_id' => $request->chat_id ]);
      event(new AppEventsPrivateMessage($message));
      return ["success"=>1,"message"=>$message];
    }

event:

namespace AppEvents;

use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
use AppModelsUser;
use AppModelsMessage;
class PrivateMessage implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    
     private $msg;
    public function __construct(Message $message)
    {
        $this->msg=$message;      
    }
    public function broadcastOn()
    {
        return new PrivateChannel('message-channel.'.$this->msg->chat_id);
    }
    public function broadcastAs()
    {
        return 'PMessageEvent';
    }
     public function broadcastWith()
     {
         return ['chat_id'=>$msg->chat_id];
     }}

channels:

Broadcast::channel('message-channel.{chat_id}', function ($user ,$chat_id ) {
    
    return  "true" ;
   
});

BroadcastServiceProvider.php:

public function boot()
    {
        Broadcast::routes(['middleware' => ['web']])
        require base_path('routes/channels.php');
    }

and app.js

  window.Echo.private('message-channel')
      .listen('.PMessageEvent', (e) => {
          console.log(e);
            $(".pres-channel").append("<b>From  privat-message-channel</b>");
      });

error:
enter image description here

Source: Laravel

Leave a Reply