Home > Net >  PHP/Laravel: Best practice to deliver search results subsequently from server to client (async)
PHP/Laravel: Best practice to deliver search results subsequently from server to client (async)

Time:09-21

In the frontend the user submits a search request. The server will then utilize multiple external APIs to gather results. Whenever an API returns a result this should be send to the client. An example are flight search engines that will show you results coming in subsequently.

I would like to handle this in Laravel, as we have worked with that for some time successfully.

Laravel Websockets: I'm concernced by that overhead with the extra WebsocketServer process run by supervisor; Also, currently it is so, that for a private channel, a user needs to be logged in, which is unacceptable for our public search website

Polling: I thought about submitting the external API searches to the queue, but my concern is, that due to the async queue worker (maybe checking every 5 seconds), the whole search takes too long; also, the queue worker is again a resource eating process I would like to avoid

What would you recommend? Thank you for your help!

CodePudding user response:

Laravel's inbuilt broadcast systems will be the best way to handle this natively. https://laravel.com/docs/9.x/broadcasting

You can either go the self hosted option using laravel-websockets, it will add some extra overhead but this will be minimal unless the site is extremely busy. Alternatively these can work with pusher where the overhead is removed, but comes with some added costs.

For authentication can see 2 options. If absolute security is a must then go private and create a user automatically with each session, potentially clearing up old users periodically. Option 2 is use public broadcasts, with each channel a random ID. If no personal data is included then obscurity rather than security may suffice.

If this approach doesn't work for you then polling every second can also work, but will produce more load on the server than sockets would.

Or final option, look at an external socket based tool without Laravels authentication requirements. Twilio Sync or similar would work, but you will need to build more of the integration from scratch.

  • Related