Hey, I’m working on something lke a game and I need to save the score at each question through the network. However, I would like this to be none blocking, so the user can continue to play without waiting the server answer, but I also have to make sure that the request are made in the correct order (not setting a score of 2 after a score of 4, and so on…). My first though was to use an async FIFO queue, but I was wondering if there is a more rescript way to do so ?
The question needs a little more context.
- You’re specifically talking about a client-server interaction
- By non blocking, you don’t want the client to wait for the server response?
- You want to maintain order
You can use a fifo queue, but that’s usually for systems to talk to one another. Not necessarily a browser client. To make non blocking, you can skip the resolve step on the client side. And continue with the rest of your code.
On the backend, assuming you have a single server instead of a distributed system, can you stay simple and maintain order on a created_at timestamp?
Your assumptions are correct, I’m talking about a client/server (mono server) communication. It will be used to save score, but I don’t want it to block the user to continue into the game while saving. Some person can have a really poor connection (or in the train, or whatever…).
I want the client to wait for the answer (showing a spinning stuff or something) and show error if they are any and manage it in some way. I need to notify the client if something bad happen. but the user should be able to play while the client/server interaction is being proceed, as in most case scenario, everything will work, but the user should not be blocked everytime.
Let’s say the user have a score of 2, sending the score to the server, then continue playing, reaching score of 4, sending the score… I want to make sure that score 2 will not erase score 4 in the case of a bad connexion of something, I indeed want to keep order. It’s an over simplification, the score will be more complexe than an integer, I will need to process
score 2 before
score 4 onn the server side.
That’s why I though about an async FIFO queue, it’s the safest way according to me. But I don’t know how I can achieve this in a functional paradigm, as it will ask to share an array of request or something…
I guess this is a react app? Any workable code example the discussion can be based on?
Also i am not sure what you are asking… promises are usually not blocking, so it’s just a matter of how you design the quiz setup (retrieve all questions) and then model an app state that allows different question-states (pending, error, scoreAvailable, unanswered).
Stepping through different questions and displaying them depending on the request state is quite straight-forward, so I don’t understand what this has anything to do with “functional” and an “async FIFO” queue?
Likewise with Ryan, I still don’t understand the difficulty here.
Keep state on the frontend super simple. Whatever the latest data it receives, use that.
- On a network refresh or reconnection, just refetch the latest data from your server. 4 should be true.
- If you want real time updates, then open up a web socket for your server to push real time changes.
If this is a toy app, just use Supabase as your API and database. Easy to setup, and the JS client has an easy query and subscribe API.