Hello, I need to listen the db in real time, how I can do it?
crontab - bad idea, because I have a sequence of running systems.
long poll?
web socket ?
You can listen to all db queries being run in the boot() method of the AppServiceProvider (or another provider)
public function boot() {
\DB::listen(function($query) {
$sql = $query->sql;
$bindings = $query->bindings;
$executionTime = $query->time;
// do something with the above. Log it, stream it via pusher, etc
});
}
I'm not sure what you're meaning, and that seems different than your original question of "listen the db in real time." The above is how you retrieve the queries in "realtime"...as they are executed by laravel.
As far as
but I received a request from an external service, how do I know if I need to process it
That would depend on how you're handling the "receiving a request from an external service." I don't know your criteria for "needing to know if I need to process it," but it seems the logic would go in that.
Simplest way is to enable query logging in the database software and then just read that file.
For instance, for MySQL there is the General Query Log (https://dev.mysql.com/doc/refman/8.0/en/query-log.html). All actions and queries are written to a file. Using command line, you can then use 'tail -f logfile' to see all queries run across your screen in real time.
@Cronix Unfortunately, this does not work, because the query is executed only when the client loads the page.
I want to reduce everything to the autonomous operation of the system. When the client filled out the form and got the result. I have a very large database and I can not get the client to wait for a response from the server.
@Thyrosis As one of the options, so maybe I'll try in my next project, I really liked the chips of the new version.
@jimmck Could you please clarify, as far as I understand, the trigger is triggered when the event occurs, but the question is how to launch the first wiretapper of the event? Create cron?
All steps are documented in the wiki, but basically are this.
- Enable mysql replication.
- Install swoole extension for php.
- Configure the binlog project.
- Start de binlog service.
- Do whatever you need with the data.