T
T
tol642021-02-17 17:49:25
Laravel
tol64, 2021-02-17 17:49:25

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++;
        }
    }
}


But notifications come to the client only after the task is fully completed.

602d2ca511dd6897112968.jpeg

602d2ccaea789468606657.jpeg

How can this be implemented so that notifications are sent immediately?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question