Caching data locally (in browser) received using Socket.io? - javascript

Is there a library/project which smooths over the process of caching some json data in the browser when using socket.io? I guess what I really mean is, is there a github/opensource project already out there focused on this task so that a developer could more or less drop it into any socket.io project?
For example, let's say I am getting tabular data for a page and the data is received by using socket.io. I want the data to remain cached so that I can save a server request if the user reloads the browser.
Additionally, I'd want this to happen more or less without me having to manually create cache variables, like: http://davidwalsh.name/cache-ajax . I want the socket.io cache library to be able to do this for me.
I want to occasionally & easily be able to clear the cache if it changed on the server. So, let's assume for that what I'm looking for has a method for analyzing timestamps on when the remote data was modified. What I mean is, let's assume for now that there is a way of notifying the browser when database table/rows/documents have been modified so that it knows when to clear the socket.io cache (perhaps sending meta information about database table modify timestamps along with data requests or maybe with an occasional comet-type message).

Why not use local storage? Read about it from Mozilla Developer Network
It is easier to use.
To set value
localStorage[ 'key' ] = strValue;
To retrieve
strVal = localStorage[ 'key' ];
Yup, just simple associative arrays

Related

Saving Files through Javascript for Local App

I am building software that runs locally on web browser. At first I was using C, but it was really hectic with HTML. So I decided to use JavaScript. This requires saving data and saving texts in plain text format. Can I do this with JavaScript? If not, how to integrate it with C or python so that I can save data?
Linux related answers will be helpful.
It depends on how/where you would like to save the data.
localStorage - A simple key/value store that lives right inside the browser. The data is specific to the computer, browser, and website, but the data persists from session to session with that particular site.
sessionStorage - Similar to localStorage, but the data is removed at the close of the session (when the user leaves your website).
cookies - if you want to instead store the data on the server, the data can be uniquely identified by a cookie stored in the user's browser. The cookie is sent automatically to the server on each request, that the server interpret and send back the desired data.
REST - You could also save your data on the server and implement a RESTful API, and use JavaScript to request the data from the server.
You cannot write to disk directly from JavaScript for obvious security reasons.

Protecting JSON in Javascript?

I used jQuery Ajax to talk to php script, then it returns JSON. Then, the returned JSON Array Object is assigned to Javascript Variable var myJSON = ajaxReturn;
Normally the returned JSON values are not visible in Page Source or Javascript File, as it is rendered on the runtime only.
But when i open the tools like, Firebug and call that variable in console, like: alert(myJSON); the results are popping out. I do NOT want it to be as this is something secret data.
What is the best way to prevent/protect the JSON at Javascript side?
Everything sent to the client side is public, this is the nature of front end development and you can't change it. It is impossible to hide stuff from the user if he decides to take a peek.
If the purpose of your application is to store the JSON for client use, then you have no way of protecting it from being accessed. However you can do all modifications upon receiving the JSON and then discard it (not store it). Keep in mind that the request can still be intercepted the response can be read simply by using the networking tab of the browser developer tools.
What do you do with the JSON data? In all probability, you are feeding UI controls or subsequent calls to web services. So if you would protect (i.e., encrypt) the JSON, you would still need client-side decryption, and so your JSON would still be vulnerable -- as you can just do an alert(decryptedJSON) too.
There is no real, safe way to protect JSON if you have to be able to decipher the data in the browser.
You can of course protect the data while it is underway over the network by encrypting it, either using HTTPS or by explicitly encrypting the data server-side and then decrypting it using client-side JavaScript. But that does not protect it from being viewed in the browser.
A better option could be to encrypt and decrypt only on the server, if that fits your scenario. So you can get encrypted JSON data from a particular web service call, then feed that data into your next web service call where it gets decrypted on the server. That way, your client-side JavaScript doesn't need to decrypt, making your data safer. But if the purpose is to populate the UI, obviously this won't fit your needs.
You have just missed the game, Once you send the data from your server then its out of your limit. Because browser like firefox can do anything, So the point is everything which renders on the client is Public.
Even if there were some way to block Firefox from displaying the data in firebug, its easy for anyone to write their own web client that pretends to be a web browser and then they can do whatever they want with the data.
If you really want to hide json-data then dont send it using ajax-json. Use diffrent terminology or server-side programming.

How can i get the HTML5 Local storage values in server side

I am a .Net developer, I know that the HTM5 localstorage is client-side storage technique. I want to get the local storage data on the server-side.
For getting cookie value from server-side we have Request.Cookie in ASP.NET. Is there any solution like that to take the local storage value directly on the server-side? Please guide me. I am using the .net 4.0 framework
Thanks,
Jibu
You will need to pass this information from the client to the server using standard HTTP techniques. Using javascript you could fill:
Hidden fields
Query string parameters
POST
Ajax call to the server
...
It will all depend on how your application is organized, what kind of information is being stored, its volume, whether you want to redirect or not, ... But in all cases this should be done using javascript since that's the only way to access data stored in localStorage.
No. The whole point of local storage is that it is local. One of the advantages of it over cookies is that you can store lots of data in it. One of the advantages of cookies is that they are tiny so the overhead of including them in every HTTP request to a given host is small. There two advantages are incompatible so you won't want them in a single technology.
If you want to get the data on the server, then you need to get the client to send it explicitly (e.g. via Ajax).
This is a widescope question. (like the length of a piece of string), but Ill try to make this helpful:
If you have values in local store in webserver I assume your webserver is JSON? Or did you use the sql local storage option?
Regardless of type of storage, you need to build an interface that both handles:
a) Reading data from your local database -> its important to involve some kind of date or index value in here if you are aiming to sync databases... this is to make sure you send IN ORDER all transactions / updates which are in your database. For this to happen you must store your data not only as tables with inforamtion but also tables that contain events of when updates happened and what was updated. (change tables). This will help check in the server end that everything is sync and also means you dont send data to the server that is not needed and can be kept locally. ((otherwise what is the point of local store if you cant save yourself server database space by only syncing waht is necessary?)
b) A HTTP local server to send the data to your destination client server or database server, etc (however you have set your infrastructure) - I recommend using industry standards for your language and server, which is Ajax and JQuery. If you do a lot of streaming of data then i recommend looking into RXjs with Ajax to get a http interface built (interface in this sense just means a way to expose your client like an API and post http calls)
c) An event loop to handle how often and what triggers the synchronization so that you dont destroy your users machine with overdoing it (you dont want to do this too often, but also want to it to be meaninful rather than "every night" maybe user enabled whenever you detect an event which triggers wifi available again.) - i recommend using native wifi reading capabilities built into Apache Cordova and also industry standards for your server setup (for example Express.js for Node.JS).
Obviously the backend server needs to have its API set up and authentication / authorizations, etc.

Offline website development using MongoDB, JSON, How to proceed?

I want to store JSON objects at client side using any Java based implementation, What are the possible ways I can try.
These objects are created and stored at form submission time when Network is not available and will be sent to server when the next time Server is connected.
How can I achieve that ? Thanks in advance,
Look at jstorage, they use a few strategies to store values. The basic is HTML5, which gives you up to 5MB storage and is widely supported.
However offline storage is what you need if user's actions need to survive page refresh or close. If they need to survive only temporary network breakdown, you will need only to keep them in JavaScript memory and try to repeat JSON requests until success.
I recommend starting from frequent repeats, and then increase the timeout (if network or server app is down for hours, there's no need for pinging it every second).

How can I cache Javascript and JSON data in my iPhone app?

I am developing a native iPhone app in Titanium.
Within this app I am using data from a remote API (which I have developed in Rails 3).
I want the user to cache the API data on their phones as much as possible.
What I need help with is the concept of caching. What is the best way of doing it? The nature of the data in the API is that it needs to be up to date. Because it is contact data that can change anytime.
I have no clue in how the cache process would work. If you someone can explain
the best way of managing a caching process for the API I would be more than happy!
I am using JSON and Javascript.
"The nature of the data in the API is that it needs to be up to date. Because it is contact data that can change anytime"
If that's true then it makes any kind of caching redundant, as you would need to compare the cache to live data to check for changes, thus making the cache itself pointless.
The only reason you may still want to cache the data is to have it available off-line. That being the case i would use an SQLite database, which is native to the iphone.
titanium-cache is clean code with a unit tests and provides some sample code in the readme. I integrated this with my own project in a matter of minutes and it worked nicely.
I think the type of cache it's application dependent.
You can cache data on:
client;
server;
other network element.
Critical point is refresh of data. A bad algorithm produce inconsistent data.
You can find interesting information on literature of distributed systems
Bye
A couple options here.
1) You can use ASIHTTPRequest and ignore cache headers to cache everything. When your app is being used, you can detect if the cache is being hit. If it is hit, you fire off a request to the server after the cache hit to request any new data. You can do this by appending a random URL param to the end of the URL since the cache keys off of the URL. If you have a good connection and new data, load it in. Otherwise do nothing and your user has the latest data when using the app under a good connection
2) Do most of #1 by always hitting the cache but instead of firing a non-cachable version of the same request to the server after hitting the cache, fire off a non-cacheable timestamp check to see if data was updated. If it has been, fire off the non-cachable full API request. If it hasn't or it fails, you can do nothing.

Categories