Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
If I'm using something like React/Vue/Angular or just simply preventing my page to load on submit with pure JS, should I generate the id for my element on the front end or should I wait for the server to respond with an id?
A simple example would be the good old todo list, I add a todo and it has to have an id so when I want to delete it I can send the id to the server. In the past I used to generate a UUID and send that to the server for it to use. My question is just if this is good practice or should I be doing it differently?
If you're going to store data in the server, you should never created the ID of an element in the client. You could add the same ID twice, or another user could use the same ID that you do, or use an invalid one, or -- you get the idea. You can almost surely avoid repeating IDS generating UUIDs client-side, but it is much easier, and more secure, to edit them in the server. Take into account that in the client you don't have any control in which the client sends to your backend. Despite what you do with JavaScript, a malicious user can always hack the request sent to the server and modify it the way they want, so you could end with IDs too long for you database fields, or with invalid values, or God know what else.
Generating ids client side is indeed bad practice for the reasons already explained. But the real point regarding your concern in my opinion is, even if you generate the ids client side, you still have to wait for that server response to make sure the resource is actually created. Server might be temporarily down, server-db connection might be down, disk might be faulty. You need that response in either case for a better user experience when there is an error. So I don't believe you're actually improving the overall user experience by generating ids client side.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
so I have more of a dilemma...
I am trying to do a simple user-name change form, where a user can change his user name... but obviously this has to be unique... now..
the usernames itself are not sensitive information, so if I expose the list of all to the user is not a big deal (I will have a simple scramble just in case)...
so the dilemma is: Should I pre-fetch all possible usernames (max there will be couple of thousands) and validate against that array each keystroke...
or do I do a (debounced) api call for every keystroke (and cache the results)?
if you think the first option is OK (given the security is not an issue), how many is too many? I would do it for up to 100, no question... but what if there are 5k, is this still OK?
or should I just do a regular one and check availability only on form submit? and return just true/false depending on availability?
It is generally a bad idea to pull a long list and then run it from local memory. The reason is that it can easily become stale which then causes significant concurrency issues. If you know the data won't change very often (like, no more than once a day) then you can get away with it but even then, not a great idea. For this reason, a debounced api call is very much preferred. I usually debounce this kind of thing at somewhere between 500-1000ms with a forced call (ignoring the timer) on events like 'enter' or blur.
Side notes:
You shouldn't be returning what usernames are taken but rather just a bool for "isAvailable" when there is no match.
Even with this methodology there is still a concurrency issue which can occur between the last validating fetch and the time the user hits enter. The uniqueness should be enforced on the db and the code should surround the db call with a try/catch in case of the concurrency hit. It is an annoying thing, race conditions always are.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So this is all more theoretical since I am sorta planning ahead here but I have a generic Parse User class that I am trying to impliment a sort of search function so one user could search for his friends user.
My initial plan was to use the Parse Query contains method to find all the users who contain 'xxx' characters in their name. However.. I noticed that parse noted this would be slow with a large database.. ideally my hope for the app would be to have thousands of users. I Know that can sound a bit ambitious but that is what I am thinking.
Is parse just not the right platform for this?
I had thought about downloading all the user objects and then using local code to filter through them quickly but that surely couldn't be faster.
Would love to hear your guys thoughts!
Your idea could work, but it could be slow. You can do some things to make it a bit faster, but contains is always going to be slow for the server to do. If you can change to 'begins with' instead of contains that will be faster. Exact matches should be faster again.
If you limit the search results that would help. So, as the user types, don't make any request to the server until 3 characters have been typed, and set the query limit to 5 results. Ideally also add a timer so if the user types a fourth character within 1 second the request isn't made. If the request is made and another character is typed cancel the current request before making a new one.
As more characters are typed in you could extend the limit to get more results.
Definitely don't download everything and search locally. Your users should also only really be accessible to cloud code because it can use the master key (your users should have an ACL which denies access to other users).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am really new to making an async game and I would like to know if you could just get the current gamestate and save it as it is or if I would have to grab all code seperatly and organising them into tables. It would be very useful if there was some kind of function that could save everything at once as I am building a pretty big game and it would take a long time grabbing every last piece of information.
So to summarise: I would like to grab the existing gamestate and put everything in a database, then grab everything again and restore that gamestate.
Thanks in advance!
You'll need to submit (or retrieve) the gamestate data to a server with JavaScript. You can use something like JQuery's ajax function for this.
For the server to do anything meaningful with your data, you'll need to use some kind of scripting language. PHP or Node.JS might be a good place to start if you're new to this.
Once the server gets the HTTP request, you can have it send a query to your database (like MySQL) and send back some kind of response.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm using Django and JQuery. I basically want to refresh team's page when the game starts (a game is started manually so the exact time is uknown). So I send an ajax request to the server asking whether the game started, but I have to do it continiously, so I'm not sure what's better:
1. Have a javascript while loop that keeps sending a request
2. Have a while loop on the server side that waits to respond until the game starts.
Or maybe there is some other better way?
Thanks!
Approach 1 which you specify is called polling and which will generate multiple calls to server so it is not recommended.
Approach 2 is not feasible since you will have time out.
Right way(efficiently) to do this type of thing is via WebSockets or some kind of Push Notification from server. few libraries are
http://socket.io/
https://github.com/SignalR/SignalR/wiki/Faq
If this had helped you don't forgot to mark it as answer :)
Your server knows when the game will start, like 5pm? Tell the browser's JavaScript to start polling at 4:59:40
Polling is simplest option since it will work with plain Django system.
There exists several other techniques to keep connection open and send messages to client (browser), like websockets or server sent events (sse), but those require additional components in infrastructure than just Django.
And you can't keep server waiting, browsers and frontend webservers usually do have time limits to detect dead connections so your connection would be terminated before game starts.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi I am trying to develop a mobile app. The problem I am having is this, when the user fills in a form on the html page I want the data to get written to an unordered list on the same page. I want when the next user fills data for that dat to get written to the second list item in the list and so on
.
Thanks in advance for any help you may be able to give with this problem
If I understood correctly, you're trying to make an app that stores the data of each submitting of the forum and returns them to all users that come across the page?
For such tasks you'll need, at least:
A web server that runs PHP, Python or other language to your liking*;
A database to connect the server-side with (MySQL or PostgreSQL usually);
What you need to do:
Learn about HTTP POST and basic SQL input/output if you haven't;
Learn AJAX if you haven't;
Store the user input in the database and have a server script retrieve it on call;
Update the dynamic page bit (the unordered list) with AJAX (loading a XML file with the PHP script that writes the database info into it should do) every minute or so using a timeout function.
I'll post links to all I've mentioned in a minute.
*NodeJS is getting popular for being asynchronous (making it easier for dynamic pages to interact with the server) and allowing programmers to use the same language client- and server-side, but I have never tried it to tell you how it fares. Edit: says John NaN: NodeJS is not recommended. Again, I don't know it myself; that is why it's on a note and not upper in the post. Good luck!
*Bonus note: don't trust W3Schools most of the time. The AJAX tutorial I linked to, however, isn't that bad and it's easy to follow.