I’m new to web development but have a programming background. I’ve created a first application with JS/HTML that essentially presents a list of events a user can register for. I want to be able to set the number of registrations remaining per event and then decrement the number each time a registration is submitted so when the next user comes along the list can be presented appropriately i.e. if no places remain for an event the the event is not presented or not selectable. In my old school programming head this would be just a lock a file for update, read the data from the file, change the data , update and unlock the file. But how do you do this in a web development scenario? any clues welcome.
Typically you use a database, reached via a server API.
There are solutions like firebase that are pitched as being easier for front end devs to get started with, but personally I always prefer to handle my own backend completely.
The later sections of the freeCodeCamp curriculum can help you get started with that.
Basically exactly the same way, although it’s a value saved somewhere (ie a database); you have to do this to ensure consistency. Note that this cannot be done client-side: there needs to be a source of truth in a server somewhere, and that’s where the locking occurs. Naïvely, can just not use locking, but IRL that doesn’t really work. The issue is when to apply the lock. So you can do some of this optimistically client-side: user registers, they see the number decrement. After or during that registration, the request goes the server to confirm. But then what happens when two people request at the same time? More critically, when both see 1 and both register at the same time who wins? Etc etc. So the lock is necessary in some form. It’s a simple example of the genuinely hard set of problems that underpin distributed systems – in this case you have a program on one server computer and some programs on multiple client computers, clients must stay up to date with (but can also each individually update) the server
In your case, I’m guessing it doesn’t matter too much, so
- client requests data from server re. places available. This is your initial HTML/JS.
- user registers.
- request made to server, server returns new data re. remaining places.
Thanks, I’ll have a look in the curriculum for some examples.