为什么 BroadCastEvent 在 Laravel 中排队?如何阻止它?
Why BroadCastEvent are queued in Laravel? How to stop that?
我正在做一个需要实时广播经纬度的项目
我有如下内容
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Redis;
class TrackersBroadcast extends Event implements ShouldBroadcast
{
public $lat, $lng,$imei,$date_time
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(
$lat,
$lng,
$imei,
$date_time
)
{
$this->lat = $lat;
$this->lng = $lng;
$this->imei = $imei;
$this->date_time = $date_time;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['tracker-channel'];
}
}
在某些情况下我需要触发实时电子邮件,所以我决定实现 laravel 如下所示的消息队列
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail;
class SendAlertEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->data=$data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
Mail::send('emails.test', ['testVar' => $this->data], function($message) {
$message->from('no-reply@sample.com', 'Website Name');
$message->to('developer@gmail.com')->subject('A simple test');
});
}
}
每当我 运行 php artisan queue:listen database
它也会对我的广播事件进行排队。我不想排队广播事件。怎么做?
也许你可以像这样在广播事件中提到队列名称,不要听那个队列,因为你不需要处理
//php artisan queue:listen --queue=broadcast-queue
/**
* @return string
*/
public function onQueue()
{
return 'broadcast-queue';
}
在Laravel中所有的事件广播都默认排队。
Before broadcasting events, you will also need to configure and run a queue listener. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected.
在这种情况下,您需要两个队列驱动程序。一个用于实时支持的事件广播。一种用于处理具有队列支持的电子邮件。为此,您需要两个队列驱动程序。您可以在 config/queue.php
中定义它们
在你的 SendAlertEmail
class 中执行此操作。
public $broadcastQueue = 'queue-name-for-handling-emails';
在您的 TrackersBroadcast
中说明实时队列。这是您的 redis
队列驱动程序。
public $broadcastQueue = 'real-time-queue';
参考Broadcast queue under Defining broadcast events
然后你可以像这样监听你的两个队列
$php artisan queue:listen --queue="queue-name-for-handling-emails"
$php artisan queue:listen --queue="real-time-queue"
参考Why event broadcast is queued ?
希望您觉得这很有用。
因为如果扩展 ShouldBroadcast
接口,Laravel 事件广播默认排队。如果您不想让事件广播排队,您应该扩展 ShouldBroadcastNow
接口。
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class TrackersBroadcast implements ShouldBroadcastNow
{
......
}
所以这意味着您的事件广播将使用同步队列驱动程序。
我正在做一个需要实时广播经纬度的项目
我有如下内容
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Redis;
class TrackersBroadcast extends Event implements ShouldBroadcast
{
public $lat, $lng,$imei,$date_time
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(
$lat,
$lng,
$imei,
$date_time
)
{
$this->lat = $lat;
$this->lng = $lng;
$this->imei = $imei;
$this->date_time = $date_time;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['tracker-channel'];
}
}
在某些情况下我需要触发实时电子邮件,所以我决定实现 laravel 如下所示的消息队列
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail;
class SendAlertEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->data=$data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
Mail::send('emails.test', ['testVar' => $this->data], function($message) {
$message->from('no-reply@sample.com', 'Website Name');
$message->to('developer@gmail.com')->subject('A simple test');
});
}
}
每当我 运行 php artisan queue:listen database
它也会对我的广播事件进行排队。我不想排队广播事件。怎么做?
也许你可以像这样在广播事件中提到队列名称,不要听那个队列,因为你不需要处理
//php artisan queue:listen --queue=broadcast-queue
/**
* @return string
*/
public function onQueue()
{
return 'broadcast-queue';
}
在Laravel中所有的事件广播都默认排队。
Before broadcasting events, you will also need to configure and run a queue listener. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected.
在这种情况下,您需要两个队列驱动程序。一个用于实时支持的事件广播。一种用于处理具有队列支持的电子邮件。为此,您需要两个队列驱动程序。您可以在 config/queue.php
在你的 SendAlertEmail
class 中执行此操作。
public $broadcastQueue = 'queue-name-for-handling-emails';
在您的 TrackersBroadcast
中说明实时队列。这是您的 redis
队列驱动程序。
public $broadcastQueue = 'real-time-queue';
参考Broadcast queue under Defining broadcast events
然后你可以像这样监听你的两个队列
$php artisan queue:listen --queue="queue-name-for-handling-emails"
$php artisan queue:listen --queue="real-time-queue"
参考Why event broadcast is queued ?
希望您觉得这很有用。
因为如果扩展 ShouldBroadcast
接口,Laravel 事件广播默认排队。如果您不想让事件广播排队,您应该扩展 ShouldBroadcastNow
接口。
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class TrackersBroadcast implements ShouldBroadcastNow
{
......
}
所以这意味着您的事件广播将使用同步队列驱动程序。