![]() Join Up! 96819 members and counting! |
|
|||
PHP & JavaScript World Domination Series: Storing data in the client.
Luis Argerich, Alejandro Mitrou
One of the things that all programmers love is to write a program which writes another program. On the web we have two different programming environments: the client (browser) and the server. Due to the HTTP protocol definition we can write a program on the server which writes another program to be executed on the client. Let's pick PHP (of course) for the server and JavaScript for the client. We'll show you in this article how you can use this scheme to store data in the client and then minimize the data transfered between the server and the browser for interactive applications like a chat room, a news system or whatever you want.
Ingredients:
The Idea:We have been trying to develop an HTTP chat room in PHP for a while. HTTP is not a good protocol for chatting but it is firewall/proxy immune, can use the potential of PHP and there's no need for Java Applets. We have had two main problems with the chat room: first since IE doesn't support the PUSH method, we needed to make it a "pull" application, (the client refreshes itself) which is unnatural for a chat application. We decided to make the client refresh time variable, the server will generate the refresh time as a function of the number of messages received in the server in the last x minutes. The second problem was deeper: since the idea is to refresh the client we would need the server to send all the messages each time, we observed that this implied heavy transfers and our benchmarks in a simulated simplified chat room model showed that this was the main cause of delays in the chat. This article deals with that second and deeper problem.
The generic model:Using frames you can refresh a given frame without reloading the others, this is good for minimizing (c/s) transfers. Our model is based on the following scheme:
In our scheme, the loader frame refreshes itself each "x" seconds - the idea is to store data in the "master" file allowing the "loader" frame to ask the server only for data that the client has not received yet. We use timestamps to mark messages, news or transferable items and know which ones need to be transfered to the client and which ones not, we use PHP4 sessions to store the "last timestamp" in the client so it is visible in the server. When the "loader" receives data it is stored in the "master" file (note that "master" is heavy but it is transfered only once) then it makes the display frame refresh. To be even more optimum we make the display frame as short as possible, the frame only calls a "display" JavaScript function which is obviously stored in the "master" file, this function uses the data stored in "master" to dynamically draw the frame. Let's review the method:
We can analyze the method as following: We have 3 files:
Master is transfered only once. Are you confused? Let's see an example: In this example we build a chat room which is not usable yet, we just show how to implement this model, please don't say "why don't you do this and that to the chat room...". You can build a chat room as complex as you want using this model if you find it useful, and remember it is not only useful for chatrooms.
Action!Please create a test database with mysql (mysqladmin create testbase) and then create a table with: create table testeable ( timestamp datetime, message text ); This is the "master" file:
Note that we have the loader frame, the display frame and a new frame called "form", this is the form we use to send messages to the chat room. Note the simple "display" function, you can use whatever you want here, colors, dynamic colors, user preferences, html tables, images, etc etc. The display frame is: <script> top.displaymsgs(); </script> We promised it will be short :-)
The loader frame is next:
Ok, then and just for your "testing" purposes the "form" frame:
Note that we make the display refresh from the "form" frame this is nice to the chat user since it sees his message and the new ones as soon as he submits his text. Users like this since it adds some dynamic to the chat mechanism. The form frame is very similar to the loader as you can see. We have presented you a powerful technique to minimize client-server transfers storing data in the client and to reduce server load making the client execute the complex display routines. You have the power, go and conquer the world.
|