PDA

View Full Version : Reloading datastores on a timer & inter client communication



jamie.nicholson
4 Sep 2008, 12:49 AM
To put my discussion in context I've just read the following thread, it explains how to use Javascript/ExtJS to periodically reload datastores.

http://extjs.com/forum/showthread.php?p=35347#post35347

This is an accepted method of refreshing datastores however it sounds very resource intensive/wasteful if you have a large number of datastores and users.

So for big applications I had different idea's inspired by Saki's blogs - big application and inter component communication, my idea is as follows

Inter Client Communication - it goes a liittle something like this

->Clients updates the server via AJAX or form submission
->Server keeps a inter client event queue - each event has an expiry time
->Client polls the inter client event queue - interval less than event expiry time
->Client then fires the event and listener functions are run if they've been setup for that client.
->All clients are happy and can update each other.

The advantage with this is that the client only refreshes or does what it needs to and providing the event queue is small it shouldn't be a huge load on the server. Although executing all the events woudl have a little overhead :-?

Someone might have done this already or have some thoughts on it, I'd appreciate your ideas here. Providing nothing better is out there I'll start on an example and add it to the thread, if it proves to be a good thing.

evant
4 Sep 2008, 2:28 AM
Just to be sure I'm clear, are you suggesting that different client machines update each other with data when necessary? Or are you saying that the server should cache results (say a dataset).

jamie.nicholson
4 Sep 2008, 3:08 AM
In short not quite, the clients wouldn't directly update each other, the server would do all the donkey work by caching an event list for each inter client AJAX transaction.


The long story


Client sessions would in-directly update each other, however it's more like the client performs say a CRUD transaction and the server does it's CRUD work and maintains a list of events. The clients would poll the server and decide what to do with the events i.e. reload/modify a ExtJS datastore. I guess the server could attach the CRUD record to the event might be less hungry on bandwidth and just modify the datastore instead of reloading it.... There could be a load of different cases

Here's a fictional example

Configuring a national grocery chain inventory application

Transaction 1 - Super user adds apple type to a list of fruits

Client 1 - Add a new fruit type 'apple'

The super user is configuring what types of fruit will be sold in the grocery chain and adds apple to the fruit types datastore. ExtJS stores the information in a back end database via AJAX, the database also registers an event entry with the datastore ID, the time it was udpated by the super user and maybe even the record object.

Client 2 - Expects to see the type 'apple'

The is another user which has been tasked with recording how many apples are in the each store. The client/browser on his machine will poll the server - event queue/table cached on the server and see that the fruit data store has been updated, it then decides to reload the fruit datastore which picks up the apple type and the end user can now see type apple in the combo list providing they have the screen open.

Other cases could be

pop messages 'server shutdown in x minutes'
force page reload for live developer release

evant
4 Sep 2008, 3:20 AM
Ok, I can see what you're saying, however a majority of the logic for that will be on the server side, so it will be difficult to integrate with Ext (in general) to be platform agnostic.

It would be cool if there was a store.loadIfUpdated() method or somethng ;)

jamie.nicholson
4 Sep 2008, 4:40 AM
Yes there would be some server side logic, similar to paging a store, you require server side logic to implement it and meet the expected JSON specification for the responseText.

The same method could be applied with the events.

jamie.nicholson
4 Sep 2008, 6:21 AM
Here's some code to illustrate the client polling the server event queue and reloading a store ... all mocked up but could be used for many events to update multiple stores and other activities.



var task = {
run: function(){
//AJAX request to server to get event queue
//expecting server to respond with JSON data in responseData
//use Ext.util.JSON.decode(response) into event object
//
//mock event object - assuming a successful response
var event = {
xtype:'store',
storeId:'countryStore',
type:'reload'
};

//mock example of a store being reloaded
//would expect to iterate over an array of events here
if (event.xtype == 'store'){
var store = Ext.StoreMgr.get(event.storeId);
if (event.type == 'reload' && store){
store.reload();
}
}
},
interval: 5000 //5 second
}
Ext.TaskMgr.start(task);


Of course when the client saved an item to the database it would need to give it a xtype,storeId,type information. The programming language and database would just shuffle the data as required and let Ext update it's interface.