#native_company# #native_desc#
#native_cta#

Storing Data in the Client Page 2

By PHP Builder Staff
on July 30, 2000

PHP & JavaScript World Domination Series: Storing data in the client.

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:

  • A “master” file which defines the frameset.
  • A “loader” frame.
  • A “display” frame.

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:

  1. A “browser” requests the “master” file. (frameset)
    The master file is transfered from the server, it defines the
    frameset and then the frames (“loader” and “display”) are
    transfered.
  2. The loader frame is parsed in the server, if the client
    has no “timestamp” session var it gets all the data from the server
    and generates JavaScript code to store the data in “master”.
    Then we set the “timestamp” session variable.
  3. The loader frame then generates JavaScript code to make the
    client refresh the “display” frame.
  4. The refresh causes the “display” frame to call a “display”
    function which generates the frame from the data.
  5. Each “x” seconds we go back to (2)

We can analyze the method as following:

We have 3 files:

“master” (very heavy, with display function and storage variables and initial values)
“loader” (light, php code to retrieve data from the server and write JS code)
“display” (VERY light, just a call to a display function in “master”)

Master is transfered only once.
Loader and display are transfered each “x” seconds.
Loader may be big the first time it is called, then it is a very short file
since only the data that the client has not received is transfered.
Display is always the same.
The “nice” display generation is processed in the client so we reduce
the server load.

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.