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

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.

Related

Stop Manipulation JS timer Client side

I am developing an quiz application which requires to calculate the exact time taken by user to give an answer for an question.
I can not implement timer for that on server side because of latency issues which may arise because for different users and their different network/service provider.
If I implement timer at client side , there are highly chances that user can edit and manipulate the time.
is there a way to create a secure timer at client side that user can not manipulate.
you can manage this storing a timestamp at the backend side when the user starts the quiz, then, with this timestamp, you can implement a client side timer, once the user finishes the quiz, the client sends an event to the backend and a "finish" timestamp is generated, so you can know how much time does the user took to answer the questionnaire by subtracting both timestamps.

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 :-)

JavaScript countdown sync with servertime

I need to develop a countdown, which synchronizes with the time on the server.
The job is pretty easy, I have a Form divided in 3 steps, the first step need to be filled and posted in 2 minutes, the last step in two hours.
I have to show the user the countdown but I also need to do this job on the server because we can't rely on the user's local time, we need to secure this countdown with the server time to prevent user's tricks. (we are on iis with asp.net webforms)
I also can't use a database to store time's information, I need to do this with session/cache variables (PM Orders)
any suggestions?
You can send your server time to client and get a difference in client time and server time and save this time difference, and also save the starting time stamp of timer.
You can verify this timer accuracy latter by hitting the server for server time and calculate new difference between client and server time, if it find any discrepancy, correct the timer.

How to get a reliable time from user

We've built a chat web-app, user get data from APIs first time and then send data back to server with those APIs, and we're using websocket to handle new data (new messages and other stuff).
Today one of our users told us that wrong number of messages showed up in the list of new messages, after hours of investigation I've noticed that user has wrong system time (about 3 minutes difference from our server time).
In our app, we check if user opened that conversation and then send current time back to server (from javascript new Date()).
I don't want to user lose any new message, even when they marked that conversation as read and during marking process new message arrived, if I set time from server they will lose that new message, if I use browser time that's not matching exact to our server.
I really don't know handling this case is important or not, and other apps handling this case or not, and if handling this how?
Every bit of help is really appreciated
Edit
I don't want to send user check-time on every request, for this reason our app sends server checked time 10 seconds after user interacted with app (marked that as read).
Send message with something like Last-Event-ID generated from server.
and decide which message you should send. not just user time.

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

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.

Categories