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

User1980's avatar

How to fire an API call faster than the time/speed of cron jobs?

Hi all,

I have to make an API that requests some data once per 1/2 second on another server. How would you do this with Laravel / PHP in general please?

Right now for example, I fire a cron job every second, and use a loop function that uses:

usleep( 500000 );  //MIlliseconds

Would this be the right way forward? Or do you know any other methods please?

Thanks,

0 likes
13 replies
Snapey's avatar

probably with a continuously running service. It's not a good idea to keep booting the framework on every interval

User1980's avatar

Do you mean that Laravel would not be a good fit for this purpose as the entire framework would boot each time?

As I need to save data to MySQL each time and perform some maths.

trin's avatar

minimal cron interval — 1 minute, not 1 second. for your case i recommended use nodejs, because it event-based lang and run demon with intervals — simple case for him

User1980's avatar

I see, so Laravel would not be a good fight for anything related to trading with exchanges then. Never used Node.js, question for you please.

Node.js is like a replacement of the PHP back end by a javascript kind of code. But can it still communicate with Mysql? Also, the demon with the intervals, do you initiate them directly within your code or it is something to setup on the server?

Thanks

trin's avatar

nodejs is js. u need create project

# mkdir ololo
# cd ololo
# npm install mysql

and simple index.js in ololo dir

import mysql form 'mysql'

const connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
})

connection.connect()

setInterval(doJob, 500)

const doJob = () => {
	console.log('do job', new Date())
	connection.query('SELECT 1 + 1 AS solution', (error, results, fields) => {
  		if (error) throw error;
  		console.log('The solution is: ', results[0].solution);
	});
}

your script make mysql connection and execute doJob function every 500 ms

Snapey's avatar

Do you need this call to the API to be made even when nobody is looking at your site?

User1980's avatar

Yes as it is for trading. ie: I check a currency, based on the results, I send another request, and so on.

User1980's avatar

I looked online, some use Laravel with Websockets to create their events. Perhaps this is a good way to keep using Laravel while still have very fast API calls.

bugsysha's avatar
bugsysha
Best Answer
Level 61

Try this package. It should solve your problem.

User1980's avatar

Wow! Perfect. I did not know about this package! Spatie thinks about everything :-)

bugsysha's avatar

Spatie thinks about everything :-)

They do, but developers misuse those packages.

Please or to participate in this conversation.