我最近在我的 PHP laravel 项目中使用了 pusher,它运行良好。我对 pusher 的了解是,它是我们的服务器和客户端之间的实时层,并创建到我们的客户端浏览器的 Web 套接字连接。我使用以下教程在我的应用程序中设置推送器:
我使用 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);
Run Code Online (Sandbox Code Playgroud)
但我认为这不是将推送器用于通知或任何其他功能的正确方法。进一步在我的项目中,我想使用 pusher 在用户的新闻提要上显示新的新闻提要而无需刷新页面,显然很少有用户会根据发布该新闻帖子的人看到这些帖子。
但是,如果客户端的条件停止显示数据,我将如何以不需要实施的方式使用推送器。在这里,我担心的是,如果我将继续向所有活动客户端发送数据,并设置 if 条件来过滤最终会降低我的应用程序的数据。
我的顾虑:
如果 pusher 向多个客户端发送数据,显然是所有活跃用户,那么它不会造成开销。
是否有任何选项可以使用渠道将数据仅传输给目标用户。
由于我是第一次实施 pusher,我对其实际工作几乎没有怀疑,因此是否有任何博客可以帮助我了解其实时工作情况。
如果我的问题不够明确和具体,请告诉我,我会进一步详细说明。
预先感谢所有尝试回答的人。
这个问题pusher-app-client-events解释说,我们可以为不同的用户创建不同的渠道,只向目标用户发送 msg。
我浏览了这个常见问题解答并了解到我们可以为一个注册的 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];
}
}
Run Code Online (Sandbox Code Playgroud)
在前端我订阅了“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);
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我们可以通过在我们的客户端代码中放置条件来将数据发送给目标用户,而不是阻止所有用户接收到的数据。
| 归档时间: |
|
| 查看次数: |
4715 次 |
| 最近记录: |