mounted:function(){// 监听channel
this.listenMessageChannel();// 初始化列表
this.requestMessage();},
methods:{
listenMessageChannel :function(){
let vm = this;
window.Echo.private('message-to-created.'+ window.Laravel.user_id).listen('MessageCreateEvent',function(e){
let message = e.message;
message.from_user = e.from_user;
vm.list_message.unshift(e.message);});},}
注册Broadcast Event源码
register broadcast
<?phpnamespaceApp\Events;useApp\Message;useIlluminate\Broadcasting\Channel;useIlluminate\Contracts\Broadcasting\ShouldBroadcastNow;useIlluminate\Queue\SerializesModels;useIlluminate\Broadcasting\PrivateChannel;useIlluminate\Broadcasting\PresenceChannel;useIlluminate\Foundation\Events\Dispatchable;useIlluminate\Broadcasting\InteractsWithSockets;useIlluminate\Contracts\Broadcasting\ShouldBroadcast;classMessageCreateEventimplementsShouldBroadcast{useDispatchable, InteractsWithSockets, SerializesModels;public$from_user;public$message;public$broadcastQueue='private-message-to-created';publicfunctionbroadcastWhen(){return$this->message->user_id==$this->message->to_user_id;}/**
* Create a new event instance.
*
* @param Message $message
*/publicfunction__construct(Message $message){$this->message=$message;$this->from_user=$this->message->fromUser;}/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/publicfunctionbroadcastOn(){$private_counter_channel=newPrivateChannel('message-to-counter.'.$this->message->user_id);$private_created_channel=newPrivateChannel('message-to-created.'.$this->message->user_id);return[$private_counter_channel,$private_created_channel];}}