When entering a record in pag1, refresh pag2

2

Good morning. This is a conceptual question. I never needed it, so I do not know if it works.

Imagine the following situation: I have a script A, where I enter information into the database. This script is running 100%.

I have a B script, where I keep track of the information entered in the database. I put a refresh every 10 seconds, every 10 seconds to update the information entered through script A.

The problem in question is as follows. I wish I did not need to have this Refresh in script B, and every time script A inserted a record in the DB, script B was updated automatically. It does not even have to be via ajax, it can be via refresh from the entire page itself.

Maybe to help in visualizing the problem, I'll tell you the application of the situation. I have a restaurant where the attendants are on the cell phone, taking the orders on the table (SCRIPT A). In the kitchen, I have a monitor where you update every 20 seconds (SCRIPT B) the items ordered in the restaurant hall.

The issue is that I do not want to keep updating every 20 seconds because there are times when the restaurant has a very quiet pace and this would generate unnecessary system consumption (bandwidth, link, system performance, etc., etc.). etc.).

Is this possible?

    
asked by anonymous 17.03.2018 / 16:18

1 answer

3

To do what you want, you need to create a WebSocket connection.

WebSocket makes a connection between a client (from a web browser) to a server. Once a WebSocket connection is established, it remains open until the client or server decides to close it. With this connection open, the client or server can send a message at any time to the other. This makes web-based communication entirely event-driven, not user-only.

In other words, communication becomes dynamic and the data consumed will be made from click events by users or some kind of change in the state of the server that will be "listening" to all connections. No need for client-side loops or "refresh" from time to time.

So, in your case, it would look something like this:

Theattendantsendstheinformationtotheserver,whichchangesitsstate,andsendsthenewinformationtothe"manager" of the kitchen which is notified almost instantly. Well as the attendant, the manager also has a WebSocket connection.

Useful links:

Tutorial Chat with WebSocket and PHP

Demo

You have a lot of information about this on google.

I hope I have helped.

    
18.03.2018 / 00:19