Answer the question
In order to leave comments, you need to log in
How to send multiple notifications to a client in one job?
Hello!
I'm trying to find a solution when it's possible to send notifications to the client while a background job is running.
class SeederController extends Controller
{
public function index()
{
$job = new SeederJob();
dispatch($job);
return response()->json(["status" => "ok"], 200);
}
}
class SeederJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
WebSocketEventService::message("Start...");
WebSocketEventService::testWebSocketProgress();
WebSocketEventService::message("End...");
}
}
class WebSocketEventService
{
static public function message($data)
{
broadcast(new WebSocketEvent([
"message" => $data
]));
}
static public function testWebSocketProgress()
{
$i = 0;
$total = 100000000;
$divisor = $total / 10;
while ($i <= $total) {
if (fmod($i, $divisor) == 0) {
broadcast(new WebSocketEvent([
"process" => "Test...",
"current" => $i,
"total" => $total,
]));
}
$i++;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question