Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm creating a single player Web rpg that will store a decent amount of information about the player, items, map, npcs, etc.

I have been developing html5, javascript, php, and the mysql database approach and saving/retrieving with json.

Someone mentioned for my purposes, using 'localStorage' persistent user storage would remove much complexity and help with performance.

Is localStorage the better way to go here? If I wanted to find out information about my players, or ensure that esch player profile is saved... can I rely on localStorage?

What are the pros and cons of each?

share|improve this question
    
What are you doing with the PHP? If you use local storage I think you'd be able to do the entire game offline and client side –  ssb May 15 at 6:39
    
How can localStorage and PHP + MySQL even compete? The first represents storage on the client side and the second represents storage on your server. Two very different things, so you'll have to decide where data has to be stored and why first. –  Alex M. May 15 at 6:41

1 Answer 1

up vote 7 down vote accepted

window.localStroage is a more modern alternative to cookies. It allows you to store (semi-)persistent data in the users web browser which will survive a browser restart. The client-sided javascript can access it without having to consult a server, which makes it quite fast to access from the client. But contrary to cookies, localstorage is not directly accessible for the server. When you need data from localstorage on the server, you need to send it to the server via AJAX or websockets after the website has loaded.

localStorage has a much higher capacity than cookies, but not unlimited. The standard suggests 5MB, but that's just a suggestion. Most browsers have a higher limit and react differently when it is exceeded. Some ask the user for permission to store more, some just drop any excess data. But you can not be sure how the browser reacts, so you should try to stay below the 5MB limit.

Keep in mind that localStorage doesn't guarantee persistence. The user can accidentally or intentionally delete their localstorage, just like they can delete cookies. Also, the web browser might have a combined hard-drive quota for localstorage for all websites and delete arbitrary data when running out.

The localstorage also doesn't guarantee integrity. The user can manipulate their localstorage. That means when you store any game state in it, you have to assume that the user will be able to edit it in any way they want, allowing them to cheat. When your game is single-player, that might not be that much of an issue. But when your game has any interaction between players (and when it's just a public scoreboard), cheaters might be quite an annoyance for the more honest players.

However, localstorage has the advantage over server-sided persistence that you do not need personalized user accounts. The data is bound to the web browser installation, so the user can save their game without having to register and log into an account. You also don't need a server-sided database which greatly simplifies your server infrastructure.

share|improve this answer
    
Philipp, perfect answer. Thank you –  Growler May 15 at 12:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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