Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mdev11's avatar

Quiz submission question by question Ajax or Websockets?

There is a quiz with 250 questions in total. The questions will remain the same so I don't pull the questions each time but for submitting I send 2 requests with Ajax: 1- Request to start counting the duration it takes the user to answer. 2- Request to submit the user's answer.

The project is hosted on a VPS server and I'm thinking of using websockets.

Should I use websockets instead?

I have never installed or used websockets so I would appreciate sharing a guide or tutorial related to this subject.

Thanks.

0 likes
1 reply
LaryAI's avatar
Level 58

Using websockets for this scenario might be overkill. Ajax requests should suffice for sending the start and submit requests. However, if you want to explore using websockets, you can use a package like Laravel Echo Server to set up a websocket server and broadcast events to connected clients. Here's a basic example:

  1. Install Laravel Echo Server:
npm install -g laravel-echo-server
  1. Set up Laravel Echo Server:
laravel-echo-server init
  1. Configure Laravel Echo Server:
{
    "authHost": "http://your-app.com",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "your-app-id",
            "key": "your-app-key"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "http://your-app.com",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}
  1. Start Laravel Echo Server:
laravel-echo-server start
  1. In your Laravel app, use the Laravel Echo package to broadcast events to connected clients. For example, to broadcast a quiz submission event:
// In your controller
public function submitQuiz(Request $request)
{
    // Submit the user's answer

    // Broadcast the submission event
    event(new QuizSubmission($user, $answer));
}

// In your event class
class QuizSubmission implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;
    public $answer;

    public function __construct(User $user, $answer)
    {
        $this->user = $user;
        $this->answer = $answer;
    }

    public function broadcastOn()
    {
        return new Channel('quiz-submissions');
    }
}
  1. In your frontend, use the Laravel Echo JavaScript library to listen for the quiz submission event and update the UI accordingly. For example:
// In your JavaScript file
import Echo from 'laravel-echo'

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});

window.Echo.channel('quiz-submissions')
    .listen('QuizSubmission', (data) => {
        // Update the UI with the submitted answer
    });

Please or to participate in this conversation.