Saving state of webpage with socket.io

Problem

I am creating a card game with node.js as the server , and i'm using web sockets (socket.io) to transfer the data from server to client .

so after the cards are dealt i want if someone refreshes the page he can see the current state of the game , in my case he just see nothing the cards are not dealt and there are no players . so is there some way to save the state of the game whenever a player refreshes the pages he can see all the changes that happen to the html page .

Problem courtesy of: ziz194

Solution

One way you can do it is to have the actual game state on the server. That way, when the user reloads the page, the page simply requests the state back from the server. This basically means that the game is actually on the server, and your clients are merely "remote controls" to the game on the server.

Another way is to save the state locally, using local storage. However, there could have been changes between the last time the user was on the game and during his return (like a card dealt, or a card drawn, a card passed etc.).

You can even use both. Where you read the local storage for the state first. That way, you have your hand state before you left. You can then request the server for the changes, and animate the game accordingly.

Solution courtesy of: Joseph the Dreamer

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.