How can I query online time servers to get the accurate time - javascript

I have found certain online time servers that share accurate time when provided with proper time zones. For example:
time.windows.com
time.nist.gov
time-nw.nist.gov
time-a.nist.gov
time-b.nist.gov
are some time servers that are used by Windows to auto-update time over internet. I want to use these servers to determine the accurate time instead of local server time or client system time. I tried querying as : http://time.windows.com/?timezone=GMT+5:30 (get request) expecting to get current time in India but it said: Error : 403.
So, I would like to know , What's the right format to query such time server to get the time & date in response. Codes using any ne of html (get)(post), php, js, jquery is/are acceptable.
Thanks !

You should not do this from your application code. As you are pointing out there are "some time servers that are used by Windows to auto-update time over internet." So, use an appropriate client program/service to set your server's time on a regular bases. This way your server's clock will always be accurate to the microsecond level. Attempting to query a time-server on a per request bases (as your description suggests) is foolish and causes a great deal of unnecessary overhead.

Related

How to send message to Slack every 'x' number of hours?

I have a configuration page where the user can select at what time to receive certain information from my API, for example they could select to get the information every day at 5PM or every Friday at 5PM. After this is set, the user should receive a Slack message at the time they defined, for example, every day at 5PM they would receive a message...
I save the user's timezone so that I can send the message to them in Slack at the correct time for them.
That being said, how can I schedule this message to send out from my node.js app? I would have a few users in the system, all who would have likely chosen different times so I would need a timer per user?
The only thing I can think of is scanning all users in the system, getting their selected time and then sending the message to them...but this doesn't seem scalable.
I'm not looking for a complete solution for this, just some pointers for how to design this sort of functionality.
I also looked into Slack scheduled messages but this isn't exactly what I'm looking for. Note: I can already send messages to Slack, I'm more interested in how to build the timer mechanism.
Thanks in advance!
EDIT:
Did a bit more research and it looks like node-schedule could be an option to schedule jobs: https://github.com/node-schedule/node-schedule#readme
With using this package, is the approach that I scan all users in my database at let's say midnight everyday and schedule jobs based on their settings...then those jobs execute at their scheduled time and the user receives the message in Slack. Is this a good approach?
For the core logic I would suggest something like this:
Store the timing of delivery (e.g. 5 AM ever Friday) for every user in your database
Then have a worker process that is running on a regular basis, e.g.
every 5 minutes
When it runs it checks if there are any due messages to be delivered
If yes it sends the message with your API information to the user and store the last time of sending for the user
This approach is resilient to downtime. It will just resume sending due messages once the worker process is running again after a downtime.
It is also scaleable: If needed you can run multiple worker processes (make sure to design your workers to support concurrent processing, e.g. with transactions)
Some additional things to consider:
Would limit the number of messages sent per run to avoid timeouts and having too many workers running in parallel
You need some error handling if sending message to Slack fails
To avoid timezone complexities I would suggest to convert all timings to UTC for processing in your app
Is this a regular task? In other words, it's executed every day at the same time for user X? If so, node-schedule seems fine, and it can run the same job regularly, you just have to set it up properly via a cron-like string (see the instructions in the README). If a user changes their setting, you then modify the previous job. The downside with node-schedule is that you need to set it up everytime your application is loaded, which can take a while and consume lots of resources if you have too many users.
Alternatively, if the number of users is big or you prefer to keep your application stateless, you can set a number of slots for sending these messages (and run that in a separate process from your main application). Let's say, a slot every 30 min. Then you set timers for those time slots (using node-schedule if you like, it will be just 48 timers), fetch the list of users for that time slot from the database, and send the messages.
Overall, NodeJS/JavaScript is pretty efficient with this sort of timer-based scheduling. If you want an in-depth dive into the reasons, see this: https://nodejs.org/de/docs/guides/event-loop-timers-and-nexttick/
You also need to consider what happens in case your application suffers from downtime. Should users be guaranteed to receive those messages, even if they are late? But that's another story :-)

Java script date object

Does the date object get the values from the operating system locally ? what if someone changes his time value , in my project I need to implement a system for reserving food, in my case the user can't make request for food before 10 am , but what if one of the users change his current time ?
I think that the best choice is to get time value form backed side.
any suggestions ?
Everything you do in javascript is client side, it's executed in the browser of the user, so your date object will be created depending on the client's browser.
Moreover, all the validation about the time has to be server side to prevent any kind of alteration. You have to perform ajax requests and make sure on your server that the client is allowed to buy food.

Chat has difference in Date.now() on server and client

I have created a chat with node.js and socket.io.
When a user sends a message, I insert it directly in the DOM and emit the message to the server, so it can be emitted to all the other clients.
The problem is that it seems the timestamp set with Date.now() on the server is different from the timestamp set on the client with exact same command.
This makes the interface a bit weird because a message sent at a later point in time can show a timestamp prior to previously sent messages.
One solution would be to calculate the time difference when the user joins the chat room and subtract this difference when a new message is added, but should this really be necessary or is this the common way to solve this problem? Could this also be the solution to cope with timezones etc?
Different time zones between the client and the server possibly. It will not work with multiple users across the world. This one will be helpful:
How to ignore user's time zone and force Date() use specific time zone
Also, if you append the timestamp to the DOM immediately, and then get the date again from the server, there will be a difference in any case because you have to consider the time that the request needs to reach the server.

analytics.js passing in timestamp

I am looking to implement Google Analytics using analytics.js rather than ga.js. I would like to implement this into my hybrid HTML5 app which is served within an iOS shell. I am aware of the iOS SDK but I am trying to maintain one code base for different platforms.
I need to consider my app recording page impressions within a disconnected scenario, so I was going to store page impressions in localStorage and then when an internet connection is detected, then send each page impression individually to the Analytics service using the ga() function call.
In theory this works but I need to be able to send the page impression timestamp with each page rather than Analytics applying the timestamp when it hits their server.
Can this be achieved?
Thanks,
Rit
You are correct the iOs sdk lets you use something called Dispatching this allows you to send data that is up to 4 hours old.
This works directly with the Measurement Protocol where it is called Queue Time
Used to collect offline / latent hits. The value represents the time delta (in milliseconds) between when the hit being reported occurred and the time the hit was sent. The value must be greater than or equal to 0. Values greater than four hours may lead to hits not being processed.
As far as I can see analytics.js doesn't allow you to do this, but it is just a JavaScript library that sends data to the Measurement Protocol. It would probably be tricky getting that to work with Analytics.js which is probably why Google hasn't added it.
My advice to you is to store your data as you say locally then send it directly to Google though the Measurement Protocol. Just remember it has to be less then 4 hours old, you will have to hope that network returns by then.

How to get client-side dateTime in server-side ASP script?

I need to get the browser user local date and Time on my server side asp vbscript page.
The goal is to show a specific message to all connected users that are in conversation at a specific local time in my web chat application.
Is that possible?
One way is to include the local time on an hidden text field and then pass it through using ajax/javascript but it's not secure because user could change it and find the secret time on server side.
The best I can think of would be to cross-reference Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"] to a time zone offset and then calculate their local time using UTC. The liabilities here are trusting that their local language setting corresponds to their time zone appropriately and that your accuracy doesn't need to be perfect all of the time (multiple time zones per language code).
Alternatively, if you can afford to be lazy in this case (how important is this local time to your app?), you could just write some purposely obscure js code to smuggle the local system's current time as a long int and named something misleading for those hackers interested in spoofing your js.

Categories