1

I'm building a multiplayer trivia game where the server is written in NodeJS and hosted on Heroku.

I ran into a problem when the server should notify the users that time's up for answering a question because Heroku reboots servers every once in awhile and can reboot my server in the middle of a game.

Is there an elegant way of solving this issue?

2
  • I would use web sockets, such as the socket.io library.
    – Andy
    Commented Apr 24, 2016 at 11:54
  • I am using socket.io, the problem is that I can't keep the state of the game. I mean, if run a setTimeout code on the server that should notify the users in 1 minute that the time's up, I can't be sure it would run because heroku can reboot my server during this minute.
    – Ephi Gabay
    Commented Apr 24, 2016 at 11:57

2 Answers 2

3

I don't know if there is a more elegant solution, but it will probably come down to persisting the question timeout and asking the database whether the value is still valid and having the client to do the time's up logic.

The first option, obviously, is to just have a client verification, where a user choses to answer a question, queries the server and gets a response in a format which could look like this:

{
    "questionId": "eJ3s9CxfAdwacCP68B",
    "question": "Who is the president of the U.S.A.?"
    "timeLimit": {
        "hours": 0,
        "minutes": 1,
        "seconds": 30
    }
}

When the data is successfuly fetched, the answer dialog is only available for as long as the timeLimit allows it. After that the input could be disabled.

Naturally this only solves the problem for people with no or very little computer knowledge, geeks will simply edit the source code of your website, enable the input again and submit their answers anyway.

This is where you need to insert server verification as well. A user asks to fetch the question with the id eJ3s9CxfAdwacCP68B, during this operation, a record in the database consisting of the user id, question id and expiration (adding the duration to current datetime) is created.

Later when a user clicks to submit the question, it sends the answer to the server and the server checks against the database value and current datetime, whether the question could actually still be answered, returning 400 when it could not.

I haven't worked with heroku, but hopefuly the database keeps its data after the reboot. If that's the case, this solution is fairly easy to implement, the time's up dialog is server ignorant and you forbid users to submit answers after their time for the question has expired.

1
  • Thanks David for this in depth comment. That's how I ended up implementing this.
    – Ephi Gabay
    Commented Apr 24, 2016 at 13:23
2

You could send a signed payload with timestamp (in addition to question id, client id etc.) to the client together with the question. When answering the question the client has to send back the signed payload. The server can then verify the answer without holding any state regarding when the question was send to the client. The client itself provides the signed state for the server. The server simply has to verify the signature and compare the timestamp with the current time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.