Search code examples
phplaravelvue.jsbroadcastlaravel-8

Laravel (+ Vue.js) Broadcast - How to set up a countdown and then send a broadcast message?


I want to create a multiplayer quiz with the broadcast system of Laravel (I use Vue.js so that the pages doesn't need refreshing). For that, I need to set up a system to stop a question (the server will send a broadcast message to the players) when the countdown is over. However I don't know how to do it correctly and with the best performances. I thought to use queued jobs: when a question is started, a job is created and queued with a delay of 20 seconds. When the job is executed (after the 20 seconds), a broadcast message is sent to players indicating the question expired.

Is it the best way to do it or is there another way that is better?

Thanks in advance for your answers


Solution

  • A queue might not be best because you might end up sending delayed responses when the queue fills up. You might be better off keeping track of time on the client (in Vue), and verifying responses on the server.

    When a question is started, store the start time. Run the countdown on the client, and display the notice when the timer has ended. When a request is sent to the server, check to see if the 20s has expired.

    You could use Websockets to help keep all players in sync.