laravel5.5 laravel echo实现实时应用(私信)

场景
  • 一个类知乎网站的私信功能
分析
实现(pusher driver)
  • 安装
    • composer require pusher/pusher-php-server “~3.0”
    • npm install --save laravel-echo pusher-js
  • 注册pusher 服务
  • 配置
    • .env 配置 pusher相关参数
    • config/app.php 注册App\Providers\BroadcastServiceProvider::class,
    • resources/assets/js/bootstrap.js 去掉关于Pusher相关的注释 && 填写相关的配置信息
    • 注册Broadcast Event源码
      • 注意channel是private
      • MessageModel中绑定$dispatchesEvents属性,自动触发事件
    • Defining Authorization Callbacks
      • Broadcast::channel('message-to-created.{user_id}', function($user, $user_id){return (int)$user_id === (int) $user->id;});
    • 监听channel
效果

效果在线查看

监听private channel
        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
<?php

namespace App\Events;

use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessageCreateEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $from_user;
    public $message;

    public $broadcastQueue = 'private-message-to-created';

    public function broadcastWhen()
    {
        return $this->message->user_id == $this->message->to_user_id;
    }

    /**
     * Create a new event instance.
     *
     * @param Message $message
     */
    public function __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
     */
    public function broadcastOn()
    {
        $private_counter_channel = new PrivateChannel('message-to-counter.' . $this->message->user_id);
        $private_created_channel = new PrivateChannel('message-to-created.' . $this->message->user_id);
        return [
            $private_counter_channel,
            $private_created_channel
        ];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值