0

I have an application (Laravel + MongoDB running on Nginx) where I pull some data from the database and render it on the screen. The application focusses on multiple real life objects. Once an object is turned on (is_on equals to true in the database), the timer on the screen needs to start ticking. Once the object is turned off (is_on equals to false in the database) the clock stops ticking and resets to 0. The format of the clock is HH:MM:SS. So it shows how long the real life object is turned on.

My problem is that I don't really now how to save/implement such timer. When the user request the page, I pull the necessary data from the database. If I also save the timer in the database, you have to make a query every second which is very bad practice.

I remembered something about WebSockets and tried to look into them. I actually managed to build a basic Hello World chat application, but don't really know how to implement this in my project. There is no place for it in the database (because of the queries), so I don't really know where to save that timer on the server. I'm also doubting if WebSockets are the way to go.

So are WebSockets the way to go and if it is, can you guys point me in the right direction on how to implement this? If not, can you advise me what I should do?

Thanks in advance!

Kevin Kromjong
  • 174
  • 3
  • 13
  • You could use a server side counter based on unix timestamp. The main thing here is to keep that counter updated just when the db operation, modifying `is_on` occurs. In your chat you can receive a tcp **push** each time the object is modified in the db. The push sender might be the Laravel code or another server side layer in charge of monitoring the db operations and send the notifications to the connected clients (desktop, app, etc.) – Evhz Mar 30 '16 at 07:59

1 Answers1

0

From your question:

I understand that the objects you print in the screen are modified by users in the application, and your aim is to live forward those modifications to other active client instances of your application.

In that case, as you mention, I would point you to websockets. They are a great way to feed information directly to the client, so the client receives the update signals and modify the interface, with no need of user action.

In order to implement the logic to notify the client, I recommend using a push approach, but this is really depending on what kind of clients you'd like to notify, since the push world is still a bit tricky.

Further readings for this websocket based push implementation:

Question about Push Flags:
Difference between push and urgent flags in TCP

If your client runs in browser or mobile this question is nice to read:
How to send push notification to web browser?

Also html5 websockets:
http://www.websocket.org/aboutwebsocket.html

As a sidenote:
A nice architecture for client-server live communication is based on node.js together with socket.io library offering good performance and not really complex implementation if you know what you do.

Community
  • 1
  • 1
Evhz
  • 8,852
  • 9
  • 51
  • 69