【问题标题】:How pusher send data to selected clients only推送器如何仅向选定的客户端发送数据
【发布时间】:2017-05-16 15:33:50
【问题描述】:

我最近在我的 PHP laravel 项目中使用了 pusher,它运行良好。 我对 pusher 的了解是它是我们的服务器和客户端之间的实时层,并创建到我们的客户端浏览器的 Web 套接字连接。 我使用以下教程在我的应用程序中设置推送器:

pusher integration with laravel

我使用 pusher 为我的 Web 应用程序创建的内容:

1.我创建了一个通知功能。当一个用户向数据库添加一些数据时,当一个用户开始关注另一个用户时,会触发一个事件,并且该事件将数据发送到特定频道说“通知频道”,并且在我的 js 代码中我订阅了这个频道。为此,我写了以下代码行:

//instantiate a Pusher object with our Credential's key
    var pusher = new Pusher('68fd8888888888ee72c', {
        encrypted: true
    });

    //Subscribe to the channel we specified in our Laravel Event
    var channel = pusher.subscribe('notification-channel');

    //Bind a function to a Event (the full Laravel class)
    channel.bind('App\\Events\\HelloPusherEvent', addMessage);
  1. 通过使用 addMessage() 函数,我显示了一些数据。现在我已经在客户端进行了检查,以便只有登录用户打算通过编写简单的 if 条件来接收此消息。由于我已在 App\Events\HelloPusherEvent 的数据中发送了预期用户的 ID,因此我使用此 ID 仅向特定用户显示消息。

但我认为这不是使用推送器进行通知或任何其他功能的正确方法。在我的项目中,我想使用推送器在不刷新页面的情况下在用户的新闻提要上显示新的新闻提要,显然很少有用户会根据发布该新闻帖子的人看到这些帖子。

但是,如果客户端的条件停止显示数据,我将如何以不需要实现的方式使用推送器。 在这里我担心的是,如果我将继续向所有活动客户端发送数据并设置 if 条件来过滤最终会降低我的应用程序的数据。

我的担忧:

  1. 如果 pusher 将数据发送给多个显然是所有活跃用户的客户端,那么它不会造成开销。

  2. 是否有任何选项可以使用渠道将数据传输给目标用户。

  3. 由于我是第一次实施 pusher,我对它的实际工作几乎没有疑问,所以有没有任何博客可以帮助我了解它的实时工作。

如果我的问题不够明确和具体,请告诉我,我会进一步阐述。

提前感谢所有尝试回答的人。

【问题讨论】:

    标签: pusher


    【解决方案1】:

    这个问题pusher-app-client-events 解释说,我们可以为不同的用户创建不同的渠道,以便仅向目标用户发送消息。

    我浏览了这个FAQ,才知道我们可以为一个注册的APP创建无限频道。

    创建多个频道不会产生任何开销。

    现在,如果我想向用户 1 发送通知,那么我将创建一个频道“notificaton-channel-1”并将用户 1 订阅到我的前端代码中的同一频道。

    我在 PHP laravel 项目中使用的事件类如下所示:

    <?php
    
    namespace App\Events;
    
    use App\Events\Event;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    /**
     * Just implement the ShouldBroadcast interface and Laravel will automatically
     * send it to Pusher once we fire it
     **/
    class HelloPusherEvent extends Event implements ShouldBroadcast
    {
        use SerializesModels;
    
        /**
         * Only (!) Public members will be serialized to JSON and sent to Pusher
         **/
        public $message;
        public $id;
        public $for_user_id;
    
        /**
         * Create a new event instance.
         * @param string $message (notification description)
         * @param integer $id (notification id)
         * @param integer $for_user_id (receiver's id)
         * @author hkaur5
         * @return void
         */
        public function __construct($message,$id, $for_user_id)
        {
            $this->message = $message;
            $this->id = $id;
            $this->for_user_id = $for_user_id;
        }
    
        /**
         * Get the channels the event should be broadcast on.
         *
         * @return array
         */
        public function broadcastOn()
        {
            //We have created names of channel on basis of user's id who
            //will receive data from this class.
            //See frontend pusher code to see how we have used this channel
            //for intended user.
            return ['notification-channel_'.$this->for_user_id];
        }
    }
    

    在前端我订阅了'notification-channel-'+logged_in_user_id

     //Subscribe to the channel we specified in our Laravel Event
        //Subscribe user to the channel created for this user.
        //For example if user's id is 1 then bind to notification-channel_1
        var channel = pusher.subscribe('notification-channel_'+$('#logged_in_userId').val());
    
        //Bind a function to a Event (the full Laravel class)
        channel.bind('App\\Events\\HelloPusherEvent', addMessage);
    

    通过这种方式,我们可以只向目标用户发送数据,而不是通过在客户端代码中设置条件来阻止所有用户接收到的数据。

    【讨论】:

      【解决方案2】:

      我认为您应该直接在 Blade 模板中添加用户 ID,而不是使用表单字段:

      var channel = pusher.subscribe('notification-channel_{{ Auth::id() }}');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-23
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多