hsbr13
hsbr13

Reputation: 441

nodejs timer on server

I am new to nodejs. I am going to develop server side of a game, in which each player has a fixed time that when it's his/her turn, the remaining time should start to countdown until he/she submit his choice.

We cannot handle this on client side because It can happen that user closes the app after some time (or turns disconnect from internet or shut doen the device) and we do not have his/her passed time and when he/she returns, he/she would see his/her last saved time in server which is not correct.

Another options is to synch client with server every for example 15 seconds, which can solve the problem somehow, but the times won't be exactly what it should be in some cases. Our game is highly dependent on time. Moreover, I guess it would have lots of overload on server and many requests from client.

Third option is to have timers for each player in server, which has extra and heavy load on server and node is single thread that makes this option impossible.

What is the best solution? tnx


update

  1. I forgot to say that for users matches, I use socket.

  2. The other point is we know when users are going to play (we determine the time) and even the number of players. I mean that it cant happen that we face a lot of users to get online and play together. We know that at 8 to 8:30 Pm, maximum 60 players would be online.

  3. This issue matters a lot because each user has a limited time to disconnect. I mean no matther how much remainig time you have, you have to reconnect after 1 minute. If you do not come back you are lost. Moreover, the whole time tou were disconnected is a penalty from your remaining time. I mean if you have 80 seconds and disconnect for 50 seconds, your remaining time is now 30 seconds.

Upvotes: 1

Views: 1838

Answers (2)

libik
libik

Reputation: 23049

You should elaborate more.

Is this 1v1 "battle"? If one player does something, does the second player need to know that happened as fast as possible?

If so and if it is not like you need to send some data every few miliseconds (it looks like you dont), I think the hosted solution should work best for you, so you dont have to care about some timeouts, disconnects etc.

You can use i.e. Firebase: https://firebase.google.com/docs/database/

Each player listens on client side on firebase so every change is propagated to other players immediately. The active changes to it should be done by sending requests to your server which then modifies the Firebase. Also it will allow anyone who disconnects to be easily reconnected and you get all the history of match.

Also think about the future - you can have multliple servers and each request can go to different server. You need one source of truth (database) - it can be Firebase, or Redis, Mongo... what suits your needs.

Beside the active servers, you should have cron server (it can be same code-base, but different deployments) that should check i.e. each second all unfinished games and either continue if everything is fine or let one player to lose if he did not replied for too long.

Upvotes: 2

Jonas Wilms
Jonas Wilms

Reputation: 138567

Why not just store the time when the user started the quiz / game, then when the user finishes the quiz, get the current time and calculate the time it took as end - start. Now on the clientside you could just request the start time once, and then show the countdown based on that.

Upvotes: 2

Related Questions