I am working on an app that has a javascript interval timer. I would like the timer to run on a server then I would like to grab that time on multiple devices on click.
Ex: Countdown Timer to be displayed on a large screen in a gym (this is coming from the server). I would like users on mobile devices to be able to capture time and record it in real-time.
If the timer on the big screen said 10:35 when I push the button on my mobile device I want it to grab 10:35 and put it into an input field.
I have the timer code figured out. I just don't know how to get the time that is displayed on the server to the other devices.
I know that this is a very general question. I am just wondering if it is possible, and if someone can point me in the right direction.
Thanks!!!
If you are using javascript/using node.js...you can use websockets to synchronize your timers in real time.
here is a link to a good tutorial :-) http://www.youtube.com/watch?v=pNKNYLv2BpQ
Hmmm. This is a particularly difficult problem given the intricacies of keeping time in sync across multiple devices.
Here's the architecture I'd suggest:
The server serves an HTML document to the clients that contains three timestamps: the start time, the end time, and the current server time. This could conceivably be fetched over AJAX.
From the current server time, the client can calculate its current skew and display a countdown timer matching the server ("big board") using client-side Javascript.
A button could then grab the value of a timer and populate the input field.
Here's a pretty similar question.
Related
I have a webpage that's for a fund raiser, and I'd like to update the amount raised live. So if a user is looking at the total raised, and somewhere else another user donates, then the first user would see the amount tick up and change to the updated amount.
I have a CouchDB for a database and Lambdas using Node.js running. How can I set this up?
It's a well known software development pattern. It's usually achieved by using "push" or "pull" approach.
In a "pull" approach a client constantly asking for data from backend. In your particular scenario it could be a timer on a page that at specified interval polls server for data and updates page if needed.
In a "push" approach backend "pushes" changes to client(s) when they become available. Web sockets would do the job in your particular case but implementing it is much more complicated than a "pull" approach.
I have a website that manages realworld tabletop games via a php, jquery, bootstrap and mysql setup.
It has been running very well for a number of years, but I am implementing a team game concept, which allows 2 "captains" to manage the pairings at the same time. The page itself does what I want it to do when one captain does all the data entry, but it is not really optimal for both to be doing it at the same time.
Once both players for a game have been selected the row turns green
The goal is that as a Captain selects a player from a drop down box, it should somehow update the other captains screen and vice versa.
Should I have some kind of timer going, and every X seconds refresh the page, form, etc? Has anyone done something similar to this in the past?
I am thinking of having a table in my database with each field on the form, and when it was last updated, then I could loop through the table and only update the most recent ones, but I feel this could be an extra layer that just may be over complicating it.
Any pointers would be appreciated
Refreshing the whole page is definitely possible, but I wouldn't recommend it.
You could execute an ajax call for every x second with setInterval(). Requesting all data from the server and see if everything is loaded. You also would have to send the new data back to the server when the player changes a field.
A better approach for this would be the usage of sockets. They synchronize data across different browsers (almost) instantly. Without the need to constantly request data from the server.
You can take a look at socket.io for more information. This is a javascript package to make the implementation of sockets fairly simple in javascript.
I am developing a multiplayer game (for scientific experiment) where participants engage in 20 rounds of interactive decision making. Each round has 3 stages, each should last maximum for 30 seconds. I wonder what would be a good way to implement the countdown.
Currently, I am using a client side approach. On the creation of the round template, I use client side timer that submits the answer of the participant when it reaches 0. This is working fine so far (because everyone starts the game at the exact same time, and the next round starts only after everyone has submitted an answer). I am not sure that this is a good way to do it, considering that participants might disconnect (go offline, close the browser, have connectivity issues) and might manipulate the sessions or something.
Would it be a better approach to do a server side timer? For instance, a collection that contains the timer, and participants subscribe to that collection? If so, how would one implement a server-side countdown? Also, would this approach cause high demand on the server, given that every second in the countdown (that we display in the template) would require listening to data on the server?
Never trust the client.
With that in mind, we need to find a way for the client to display the remaining time, according to the time the server chose... First, the server puts the end-time of a round when it is created (or start time + duration).
Now that everyone has the same end-time which is according to the server, we need to sync them with server time. Let's use mizzao:timesync it's pretty straightforward, it receives the server time, and creates a difference from the client time. Monitors the client time to make sure no weird clock changes occur and even considers the latency. This might be a bit more than what you need, but it's already done, so less work for us!
Now that we know the current server time, and the round end time, we can easily show how long we got remaining! If a player comes back after a disconnect or refresh, both of those times will still be valid and they'll be able to continue the game.
I am making a realtime puzzle game using Node.js and Socket.io in the backend and Angular in the frontend. I need to keep track of time that the user spends on the puzzle and send it to the backend once they complete the puzzle OR if the time runs out before. I am not sure what the best approach for this would be.
My initial thought is just to keep track the time on the client side, which is easy to do. I would then send the time once the user completes the puzzle OR the time runs out. I think this would not be very secure since the client can easily slow down their computer or do something else to send false data.
My other idea is to just keep track of the time on the server side for EACH socket/player. I would also have to update the client side by emitting a message to the client after every second or something. This way is definitely secure because the client would never send me the time information. But this approach has other issues, how would this scale?
My last idea is to use a combination of both techniques. For example, I'll have a counter on the client side that shows the client the time so I don't have to make the server "emit" a time message every second or something. I would keep track of the time on the sever side, and simply use that information to update the score etc.
Thanks
The third option definitely sounds best to me. Since you're using Node on the back end, you get access to setTimeout just like you do in the browser. You can use that to schedule an event to be emitted to the client when their time is up, and meanwhile use front-end code to display the time remaining. You'll just want to make sure you don't start the clock on the front end until you've received confirmation that the timer has started on the server, and maybe pad the server timeout slightly to ensure that you don't cheat your player out of any time if things gets slightly out of sync!
Your third option is definitely the best. The only question is: how precise does it have to be? I don't know if people are going to compete for high scores based on time, but it would suck if higher latency would lead to a worse time. Personally I believe you could accept this for puzzle games, because it would otherwise become quite complicated. One solution for this could be to initially accept the (possibly tainted) client side measured time, but check it with the server time with a certain margin (+/- 1 second for example).
I fully understood what you were going to implement
I think you must implement the tracking in server, not frontend
As you know, the users can send the fake request
So in this case, you can use the websocket method
If you use the websocket method, we can check the user's played time
For example, we know when users start and end the game
so I think you can count the while time
I hope the websocket method is good way for you
Thanks
I am building a web app using this calendar.
Jquery Based Full Calendar
This calendar is based on jquery and jquery is client side code so it works on client side time.I want this calendar to use my php server time for its whole working.
So which would be the best method for this.
Should i get server time every time once this calendar gets loaded
and keep it counting using that time .
Or should i sync it with server at every 57sec and get server time.
Make your users select a time zone and calculate their time with UTC time you get from your server. Most of the websites use this system as I know.
You should take a look at this question - it should have a lot of useful information for you in regards to your question about syncing time.
That said, think about how your application scales and work from there. A request per minute can easily add up to a load that isn't easy to handle.
Do you really need real-time syncing of time events if you're just going to have people schedule events at a future period of time/date? If this is for a calendar, you really just need to make sure that the client's time matches your at the hour and minute level, and that the dates are the same. After that, you can rely on the client's time, unless you're scheduling by the second. (Then again, I don't know what this calendar is for...)
Additionally, if you're saving their events server side, I'm not sure that it would do you any good to sync time in the way you're considering. If you're looking to push updates to all calendars at once when someone makes a change, you might look into alternative ways to do that.
Good luck - I hope this helps.