推送器如何仅向选定的客户端发送数据
How pusher send data to selected clients only
我最近在我的 PHP laravel 项目中使用了 pusher,它运行良好。
我对 pusher 的了解是它是我们的服务器和客户端之间的实时层,并创建到我们的客户端浏览器的 web 套接字连接。
我使用以下教程在我的应用程序中设置推送器:
pusher integration with laravel
我使用 pusher 为我的 Web 应用程序创建的内容:
1.I 已创建通知功能。当一个用户向数据库添加一些数据时说当一个用户开始关注其他用户时触发事件并且该事件将数据发送到特定频道说 '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);
- 通过使用 addMessage() 函数,我显示了一些数据。现在我已经在客户端进行了检查,以便只有登录用户才打算通过编写简单的 if 条件来接收此消息。因为我已经在 App\Events\HelloPusherEvent 的数据中发送了目标用户的 ID,所以我使用这个 ID 只向特定用户显示消息。
但我认为将推送器用于通知或任何其他功能并不是正确的方法。进一步在我的项目中,我想使用推送器在用户的新闻源上显示新的新闻源而无需刷新页面,显然很少有用户会看到那些 posts 根据谁 posting 新闻 post.
但是如果客户端停止显示数据,我将如何以不需要实施的方式使用推送器。
在这里,我担心的是,如果我将继续向所有活动客户端发送数据并设置 if 条件来过滤最终会降低我的应用程序的数据。
我的顾虑:
如果pusher向多个client发送数据,明明都是活跃用户,会不会造成开销。
是否有任何选项可以使用通道将数据仅传输给目标用户。
因为我是第一次实现推送器,所以我对它的实际工作没有任何疑问,所以有没有任何博客可以帮助我了解它的实时工作。
如果我的问题不够清楚和具体,请告诉我,我会进一步阐述。
提前感谢所有尝试回答的人。
这个问题 pusher-app-client-events 解释了我们可以为不同的用户创建不同的渠道,以便只向目标用户发送消息。
我通过这个FAQ了解到我们可以为一个注册的APP创建无限的频道。
创建多个频道不会产生任何开销。
现在,如果我想向用户 1 发送通知,那么我将创建一个频道 'notificaton-channel-1' 并在我的前端代码中将用户 1 订阅到同一频道。
我在 PHP laravel 项目中使用的事件 class 如下所示:
<?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);
这样我们就可以只向目标用户发送数据,而不是通过在我们的客户端代码中设置条件来阻止所有用户接收数据。
我认为您应该直接在 Blade 模板中添加用户 ID,而不是使用表单字段:
var channel = pusher.subscribe('notification-channel_{{ Auth::id() }}');
我最近在我的 PHP laravel 项目中使用了 pusher,它运行良好。 我对 pusher 的了解是它是我们的服务器和客户端之间的实时层,并创建到我们的客户端浏览器的 web 套接字连接。 我使用以下教程在我的应用程序中设置推送器:
pusher integration with laravel
我使用 pusher 为我的 Web 应用程序创建的内容:
1.I 已创建通知功能。当一个用户向数据库添加一些数据时说当一个用户开始关注其他用户时触发事件并且该事件将数据发送到特定频道说 '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);
- 通过使用 addMessage() 函数,我显示了一些数据。现在我已经在客户端进行了检查,以便只有登录用户才打算通过编写简单的 if 条件来接收此消息。因为我已经在 App\Events\HelloPusherEvent 的数据中发送了目标用户的 ID,所以我使用这个 ID 只向特定用户显示消息。
但我认为将推送器用于通知或任何其他功能并不是正确的方法。进一步在我的项目中,我想使用推送器在用户的新闻源上显示新的新闻源而无需刷新页面,显然很少有用户会看到那些 posts 根据谁 posting 新闻 post.
但是如果客户端停止显示数据,我将如何以不需要实施的方式使用推送器。 在这里,我担心的是,如果我将继续向所有活动客户端发送数据并设置 if 条件来过滤最终会降低我的应用程序的数据。
我的顾虑:
如果pusher向多个client发送数据,明明都是活跃用户,会不会造成开销。
是否有任何选项可以使用通道将数据仅传输给目标用户。
因为我是第一次实现推送器,所以我对它的实际工作没有任何疑问,所以有没有任何博客可以帮助我了解它的实时工作。
如果我的问题不够清楚和具体,请告诉我,我会进一步阐述。
提前感谢所有尝试回答的人。
这个问题 pusher-app-client-events 解释了我们可以为不同的用户创建不同的渠道,以便只向目标用户发送消息。
我通过这个FAQ了解到我们可以为一个注册的APP创建无限的频道。
创建多个频道不会产生任何开销。
现在,如果我想向用户 1 发送通知,那么我将创建一个频道 'notificaton-channel-1' 并在我的前端代码中将用户 1 订阅到同一频道。
我在 PHP laravel 项目中使用的事件 class 如下所示:
<?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);
这样我们就可以只向目标用户发送数据,而不是通过在我们的客户端代码中设置条件来阻止所有用户接收数据。
我认为您应该直接在 Blade 模板中添加用户 ID,而不是使用表单字段:
var channel = pusher.subscribe('notification-channel_{{ Auth::id() }}');