推送器如何仅向选定的客户端发送数据

时间:2017-01-02 14:04:18

标签: pusher

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

pusher integration with laravel

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

1.我已经创建了通知功能。当一个用户向数据库添加一些数据时,如果一个用户开始关注其他用户,则触发一个事件,并且该事件将数据发送到特定通道,例如'notification-channel',并且在我的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仅向特定用户显示msg。
  2. 但我认为这不是使用推送器进行通知或任何其他功能的正确方法。此外,在我的项目中,我想使用推送器在没有页面刷新的情况下在用户的新闻源上显示新的新闻源,显然很少有用户会根据谁发布新闻帖子来查看这些帖子。

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

    我的担忧:

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

    2. 是否可以选择使用渠道将数据渠道化为目标用户。

    3. 当我第一次实施推动器时,我对其实际工作几乎没有疑问,所以是否有任何博客可以帮助我了解它的实时工作。

    4. 如果我的问题不够明确和具体,请告诉我,我会进一步详细说明。

      提前感谢所有想要回答的人。

2 个答案:

答案 0 :(得分:3)

此问题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];
    }
}

在前端我订阅了&#39; notification-channel - &#39; + 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);

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

答案 1 :(得分:0)

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

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