When entering a record in Pag1, update the pag2

Asked

Viewed 93 times

2

Good Morning. This is a conceptual doubt. I never needed it, so I don’t know if it works.

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

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

The problem at hand is this. I wish I didn’t need to have this Refresh in the B script, and every time the A script inserted a record in the BD, the B script was automatically updated. It doesn’t even have to be via ajax, it can be via refresh of the entire page.

Perhaps to help visualize the problem, I will talk about the application of the situation. I have a restaurant where the attendants stay by phone, taking orders at the table (SCRIPT A). In the kitchen, I have a monitor where is updating every 20 seconds (SCRIPT B) the items ordered in the hall of the restaurant.

The point is that I don’t 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 consumption, link, system performance, etc.. etc. etc...)

Is that even possible?

  • 1

    Perhaps creating a file on the server when the A script is called. The kitchen page would be checking if a new file exists, and if YES, would do a refresh on the B script. Of course this would cost the server requests every second, but in return it would save the BD unnecessary requests. Perhaps there is something more efficient and better at lower cost. I believe that just a check if a file exists would not be so costly to the system.

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 each other. This makes web communication entirely directed by events, and not exclusively by the user.

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

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

inserir a descrição da imagem aqui

The attendant sends the information to the server, which changes its status, and sends the new information to the "kitchen manager" who is notified almost instantly. As well as the attendant, the manager also has a Websocket connection.

Useful links:

Tutorial Chat with Websocket and PHP

Demonstration

There’s a lot of information on this on google.

I hope I’ve helped.

  • 1

    Excellent. Thank you.

  • Tranquil @Novatodaweb =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.