0

I am currently making a simple clock in and out system for staff. The warehouse has a TV with a list of names on the right of people who have not signed in, and a list of names on the left of those who are signed in. When they clock in on the scanning device downstairs I need the TV/dashboard upstairs to update and move their name onto the appropriate side.

I have made the function that logs the details into the database. And I know I could use code such:

<meta http-equiv="refresh" content="3;url=thecurrentpagesurl" />

And doing that would refresh the page run a function that checks for changes and updates the display accordingly, however I was wondering if there was a way of "listening" for a change without having to spam the server to often. Ideally I want the dashboard to update in real time.

Using meta refresh I can refresh the page use a function to check for db changes and update the html on the dashboard accordingly. Is there a less resource intensive or more efficient way of doing this?

(I'm not a JavaScript expert but I have enough understanding to use some at a basic level).

Edit

This question has asked before and I have looked at the other answers which is why I came to the conclusion about the meta refresh aspect, but what I wanted to know if there was a more efficient way of doing it given my specific set up.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mike Abineri
  • 409
  • 2
  • 13
  • 4
    Have a look at websockets or longpolling. – Jan May 29 '19 at 10:20
  • 2
    AJAX or WebSockets are your principal options here. But really, how often do people actually clock in and out? And how fast do people need to be notified of that (i.e. what do they actually do with that info and will it be a problem if they don't find out immediately?). What I mean is, do you really need to update it every 3 seconds? Maybe every couple of minutes (or even less) would be fine for all practical purposes. If so then you can simply change the time on your refresh command, and the "constant" refresh issue goes away without you having to learn any new concepts or write any new code. – ADyson May 29 '19 at 10:21
  • 3
    Depends, this is simple and it works, maybe 3 secs is a bit short, set it at 60. I wouldnt to worry to much about server load. – Grumpy May 29 '19 at 10:21
  • P.S. for anyone reading in future, we forgot to mention server-sent-events (SSE) as an option here as well, since the communication appears to be only one-way. – ADyson May 29 '19 at 10:45

2 Answers2

1

In your described environment your current approach is not bad.

Here are some ideas/options on which you can read up and improve your application if needed:

  • Use Ajax and just refresh the content of your lists. You can combine this with neat animations. But this is still not real-time. This is especially easy when using jQuery or similar libraries.
  • You could use Ajax with longpolling, This means that the Ajax-Request will be hold open on the server-side until any change is happening (or a possible timeout). When a request got an response just open another ajax request with longpolling and wait for more changes. This is not realtime, but it gets really close to it. In some environments this can stress the server since each open request will occupy resources or block sockets/threads etc.
  • Another more modern version is to use the Websockets. This is the most complex approach, but the communication is in realtime. The Browser-view and Server are establishing a tcp connection and keeping that open to communicate over it. There are also some good open-source libraries for Websockets.

In your described situation i would go with your current solution and also take look into the first two ideas. They are not that complex and can give the feel of "realtime" updates.

Jan
  • 2,853
  • 2
  • 21
  • 26
0

I decided to run the refresh at set times, so what I did was use php date function to calculate the time, if the time is between 7-9 (the earliest staff clock in) or 5-7 (the latest anyone clocks out) it will run the meta refresh that way it doesnt check all day while everyones at work singed in. Thanks again !

Mike Abineri
  • 409
  • 2
  • 13