How to check real-time change in the database with websocket php?

I want to update a div only when there is a change in a certain field in the database, I'm currently using polling, but I want to switch to websocket for performance reasons, but I only found examples of chats, and that's not what I need.
Does anyone have a tip on how to check database changes with websocket?

Author: UzumakiArtanis, 2017-03-29

2 answers

You have several ways.

  1. MySQL: Using sys_exec().

    It can be used as a Trigger, whenever the data is added It is triggered and therefore can trigger a file, such as a PHP, this PHP sends the information via websocket.

    Curse: PHP tends to be slow and added up with the connection time you will have a much slower query, since it will only end after websocket is completed, or you will have to do some gambiarra to run silently , either way this will not be mentioned in EXPLAIN, you may not be able to know the reason the database is slow.

Found this PubNub post, a company that offers websockets , which uses curl directly using sys_eval(), but with a "warning" that this is not practice.

  1. MySQL: check the logs.

    Do you know how MySQL replicas are synchronized (a master + X slaves)? One of the ways is to synchronize the logs of the master, it contains everything that has been changed and you can use this to see what has changed and send information based on it. This solution is perhaps the most complex (and less clear) in my opinion, you can also use something like inotifywait to trigger when the file is updated.

  2. PHP: send after commit.

    Imagine that you have a website that has a balance, transferable between users, you want to notify them when each one receives and sends, for example :

mysqli_begin_transaction($con);
mysqli_autocommit($con, false);

$detalhe = mysqli_query($con, '
   INSERT INTO `pagamento_detalhe`(`QuemPagou`, `QuemRecebeu`, `Valor`) 
     VALUES ("'.$idPagou.'", "'.$idRecebeu.'", "'.$Valor.'")
');
$detalhe = mysqli_affected_rows($con) === 1;

$atualizaSaldo = mysqli_query($con, '
   UPDATE usuario as Pagou, usuario as Recebeu 
    SET Pagou.Saldo = Pagou.Saldo - '.$Valor.', Recebeu.Saldo = Recebeu.Saldo + '.$Valor.' 
     WHERE Pagou.id = "'.$idPagou.'" AND Recebeu.id = "'.$idRecebeu.'"
');
$atualizaSaldo = mysqli_affected_rows($con) === 2;

if($atualizaSaldo && $detalhe){
    mysqli_commit($con);
}else{
    mysqli_rollback($con);
}

It would be easier to add something like this:

//...
if($atualizaSaldo && $detalhe){
    mysqli_commit($con);
    enviar_websocket($idPagou, 'Você enviou '.$Valor);
    enviar_websocket($idRecebeu, 'Você recebeu '.$Valor);
}else{
    mysqli_rollback($con);
}

Creating a function enviar_websocket will send the websocket to the corresponding id with a message which you define, for example. This will be sent whenever a data update occurs, but still on the PHP side and not MySQL.

 6
Author: Inkeliz, 2017-03-31 18:48:50

You need to identify who does the update, delete or insert in your bank, probably a file .php should do this, considering this, some client machine requested this file, correct?

Considering that you already have a websocket server working, just send ()" javascript " to the socketID of the client machine that wants to receive this change.

For detail on how to mount websocket server, I have a fully in php and I can help you, is not it difficult, there are several tutos that teaches how to set up one as chat, just adapt to its use. see this link http://gustavobeavis.github.io/blog/2014/09/11/web-socket-php /

I relied on it to set up an internal chat for the company where I work...

 0
Author: FABIO MATEUS, 2017-08-16 18:02:08