Search code examples
laravelpusher

Laravel pusher how work without broadcastAs


In my old version of Laravel, messages were sent this way

class NoticeEvent implements  ShouldBroadcastNow
{

  use Dispatchable, InteractsWithSockets, SerializesModels;

  public $message;

  public function __construct($message)
  {
      $this->message = $message;

  }

  public function broadcastOn()
  {

      return ['channelName'];

    
  }

  public function broadcastAs()
  {
      return 'eventName';
  }
}

This method worked great. But I updated laravel to version 10.48.4 where there is no method broadcastAs and broadcastOn return PrivateChannel object. It is impossible to send a message to a pusher from Laravel

class NoticeEvent implements  ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

   
    public function broadcastOn(): array
    {


        return [
            new PrivateChannel('channelName'),
        ];

    }


}

I read the documentation, everything is described in detail there But I just don’t have methods that can be overridden, for example broadcastAs(). If you open the source files Illuminate\Contracts\Broadcasting; which is located at /vendor/laravel/framework/src/Illuminate/Contracts/Broadcasting it has only one interface interface ShouldBroadcast with only broadcastOn() This file describes one interface with one method, and it is not clear to me how to implement others that are in the documentation.


Solution

  • class NoticeEvent  implements ShouldBroadcastNow {
    use Dispatchable, InteractsWithSockets, SerializesModels;
    
    
    public function __construct($message)
    {
        $this->message = $message;
    }
    
    
    public function broadcastOn(): array
    {
        //public channel name
        return [
            new Channel('channelName'),
        ];
    }
    
    public function broadcastAs(): string
    {   
        //event name
        return 'eventName';
    }
    
    public function broadcastWith(): array
    {   
        //if you need to send a message
        return ['message' => $this->message];
    }
    

    }

    call from code

    event(new NoticeEvent("message text"));