Smartupdater calls this function after maxFailedRequests number of AJAX
224 | errors and passes ajax object as the 1st parameter and error info as 2nd and 3rd parameter ( see jQuery AJAX for details)
225 |
226 |
By default Smartupdater is started automatically by itself imideately after it was created.
342 | If you don't need to run polling process imideately, you can
343 | disable self start by setting this option to false.
344 | Use smartupdater("restart") to start smartupdater.
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
List of available features
357 |
358 |
359 |
360 |
361 |
362 | Remote Timeout - Set timeout remotely from server
363 |
OK. Now let's create something really cool. For example - automatic control system, similar to space missile control... well, Volvo cruise control, why not? Maybe a bit simpler, however :)
74 | Sometimes, we want to keep clients' polling rate within pre-defined range to prevent server overloading from one hand and loosing customers data because of too slow polling from the other.
75 |
76 |
77 | As we know, key point for every automatic control system is feedback. And Smartupdater has this feature. Every cycle Smartupdater sends current polling rate to server and (as you already know) server can re-program Smartupdater (see "Remote Timeout").
78 | To organize feedback Smartupdater uses custom HTTP header X-Smartupdater and sends data with this header
79 | data: JSON
80 |
81 | In this example server keeps Smatrupdater timeout within range 5 - 15 sec. If you set it less then 5 sec, server re-set it to 5 sec in the next polling cycle. If you set timeout greater then 15 sec. server re-set it to 15 sec in the next cycle. No Smartupdater setting is needed.
82 |
68 | Complex alternation let you to associate URL with dedicated callback function and dynamically apply this configuration.
69 | For example, if your module is opened, you poll url "getData.php" and process result with callback function "processData()".
70 | But if the module is minimized, you poll url "getAlert.php" and process result with callback function "processAlert()".
71 | Using this feature you easily can create event-driven polling engine.
72 |
73 |
In this example you can see how to switch between two Smartupdater configurations.
74 | It's not necessary to restart Smartupdater after it was
75 | altered, but if you want to update view immediately, do restart it.
76 |
77 |
63 | If you develop complex application, very often you need to process the same data differently depend on
64 | current layout, state, user preferences or other conditions. With Smartupdater you can easily swithch among multiple
65 | callback function, which makes your code very modular, flexible and clean.
66 |
67 |
In this example you can see date in regular and reverse order. It's not necessary to restart smartupdater after it was
68 | altered, but if you want to update view immediately, do restart it.
69 |
70 |
At any time you can set any timeout which is right for current task your application is executing. See how it works in this demo. (Set interval in seconds, not milliseconds)
53 | Another case: depend on current layout, state, user preferences or other conditions you want to request different data.
54 | For example, user iconized module: you don't need request regular data any more, but want to poll alerts or notification only.
55 | With Smartupdater you can easily switch among multiple URLs, which makes your code very modular, flexible and clean.
56 |
57 |
In this example you can poll either regular getsec.php or funny reverse date layout. It's not necessary to restart smartupdater after it was
58 | altered, but if you want to update view immediately, do restart it.
59 |
60 |
If for some reasons URL doesn't response, you don't want to continue to poll it, right? Smartupdater takes care of it. Use "maxFailedRequests" option to set max allowed number of consecutive ajax failures and Smartupdater stops polling after this number of failed requests
50 |
In this example maxFailedRequests is set to 3, Smartupdater polls url which doesn't exist and gets errors as return. It stops after 3 polling cycles (15 sec).
51 | This feature let you to save traffic. It allows the request to be successful only if
52 | the response has changed since the last request. This is done by checking the Last-Modified header.
53 | This technique also checks the 'etag' specified by the server to catch unmodified data.
54 |
55 |
56 | Those who are familiar with HTTP caching and already looked into the smartupdater source probable would
57 | like to ask me a couple of questions:
58 |
59 |
60 |
61 | Q. jQuery AJAX already has "ifModified" option to activate http cache feature.
62 | So, why do you re-invent the wheel?
63 |
66 | Q. You are talking about "304 Not Modified" http header, right? But neither your code example, nor your live demo
67 | shows any "304 Not Modified" http headers. LOL :)
68 |
69 | A. Yes, it's funny :) But there is another jQuery AJAX bug (or maybe not bug, but problem which is known as "Opera issue").
70 | Briefly, the problem is: Opera's xhr object returns "304 Not Modified" header in non-standard format, so that jQuery AJAX engine can't interpret it correctly. That's why to avoid using "304" header, I have to implement caching algorithm directly in Smartupdater.
71 |
72 |
73 |
Please pay attention to the PHP code: Server doesn't send "304 Not Modified" header, but includes ETag header
74 | in each reply. Smartupdater gets this ETag, compares with previous one and if they are equal, process response
75 | as if it gets "304 Not Modified" header.
76 |
77 |
78 | Two http headers are used by Smartupdater to implement this feature:
79 | ( ETag / If-None-Match) and ( Last-Modified / If-Modified-Since). They are processed identically, but ETag is more
80 | suitable for cache validation (check if content is the same), while Last-Modified more suitable for checking cache
81 | freshness ( check content time or revision number).
82 |
83 | Server sends headers ETag or Last-Modified and Smartupdater returns the data in If-None-Match or If-Modified-Since headers.
84 |
85 | Open debugger and check network activity. You see, that client sends requests every 5 seconds, but server responses with actual data only once in a minute. The rest server's responses have header and empty message body.
86 |
87 |
Smartupdater plugin performs periodical updating functionality and can be used in all sorts of applications which need polling mechanisms.
4 |
5 | Smartupdater 4.0 has exactly the same functionality as version 3.2. The difference is API style. Because API has been changed, use this version only for new projects. Don't upgrade existing projects with version 4.0 as APIs 3.xx and 4.0 are incompatible.
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
Smart Actions:
15 |
16 |
Stop/Restart polling
17 |
Change polling timeout dynamically
18 |
Switch polling URL dynamically
19 |
Switch callback function dynamically
20 |
Remotely set polling timeout
21 |
Remotely select callback function
22 |
23 |
24 |
Smart behavior:
25 |
26 |
Polling:
27 |
depend on element's size, current layout and visibility client automatically stops and restarts polling
28 | new in 3.1
29 |
30 |
31 |
Updating:
client GUI is updated only if new data is different from the old one.
32 |
33 |
Errors:
stop polling after predefined number of failures and call
34 | callback function to handle this event.
35 |
36 |
37 |
38 |
39 |
Traffic:
support "304 NotModified" http caching with "ETag" and "Last-Modified"
40 |
41 |
Feedback:
sends current timeout to server as a feedback which allows server to protect itself from overloading by increasing timeout and/or organize automatic control to force client to keep timeout within a valid range.
42 |
43 |
Smart Server:
depend on result and other conditions, server can specify which callback function on client side should be called to process this particular data.
44 |
45 |
Smart Client:
46 |
depend on application's state, current layout, etc., client can specify which url should be requested at this particular moment.
47 |
48 |
49 |
50 |
51 |
52 |
Smart Monitor:
53 |
54 |
state
55 |
timeout
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
List of available functions
64 |
65 |
smartupdater("stop") - to stop polling
66 |
smartupdater("restart") - to run polling
67 |
smartupdater("setTimeout",timeout) - to set a new polling interval
68 |
smartupdater("alterUrl"[,url[,data]]) - to alter URL
69 |
smartupdater("alterCallback"[,callback]) - to alter callback
70 |
71 |
72 |
List of available attributes
73 |
74 |
smartupdater("getState"); - current state (ON|OFF|undefined)
75 |
smartupdater("getTimeout"); - current timeout
76 |
77 |
78 |
79 |
List of available options
80 |
81 |
url - URL for the ajax request. (Required!)
82 |
type - Can be either GET or POST
83 |
data - Array of values to be passed to the page
84 |
dataType - Response type
85 |
minTimeout - Value for the timeout, in milliseconds.
86 |
maxFailedRequests - smartupdater stops after this number of consecutive ajax failures;
87 |
maxFailedRequestsCb - function to handle "maxFailedRequests" event.
88 | new in 3.2
89 |
httpCache - Activate http caching feature
90 |
rCallback - List of callback functions which are allowed to run remotely from server
91 |
smartStop - Activate feature which automatically Stops/Restarts Smartupdater depend on visibility and size of element Smartupdater is attached to.
Data structure and data format which server sends to client can vary. For example, server sends client only new data it gets since last response. But occasionally old data can be changed (something deleted or modified) and server need a way to instruct client to process this particular piece of data differently.
59 |
60 |
Smartupdater lets you to set callback function from a server.
61 |
62 |
Instead of adding extra field (command field) to the data frame, parse the field on client side, extract command and process data according this command, you can simply instruct Smartupdater which callback function should process this piece of data.
63 |
64 | In this example server checks current seconds and if it less then 40 sec. function foo() as a callback function, otherwise function bar() is set.
65 |
66 | To activate this feature set option rCallback with list of of function which server is allowed to set as callback. This list is a string with comma-separated function names.
67 |
68 |
69 | To implement this feature Smartupdater uses custom HTTP header "X-Smartupdater". Value for the header is built in JSON format
70 |
Everybody who has deal with polling systems knows, that server can be easily overloaded with client-side requests, so we try to set polling period as low as possible. But if number users grows, we have to either increase polling period and make our application less dynamic or spend money on either upgrading hardware or buying additional resources from service provider.
51 |
52 | Smartupdater lets you to re-program timeout from a server.
53 |
54 |
If server is overloaded with user's requests, it can
55 | increase polling period.
56 |
An opposed case: for some reason data is changing more quickly then usually, but current polling period is too long, so that user is loosing important data. In this case server can decrease polling period and returns to regular rate later.
57 |
58 |
59 | In this example server checks current minute and if it even sets timeout to 20 sec.,
60 | and if it odd sets it to 5 sec (shown in milliseconds).
61 |
62 | To implement this feature Smartupdater uses custom HTTP header "X-Smartupdater".
63 | Value for the header is built in JSON format
64 |
89 | It's simple: automatically stop polling if the result can not be visible and restart it as soon as it can be visible again.
90 | But when result can not be visible?
91 |
92 |
93 | - module is minimized or set too small to display result.
94 | - module is invisible ( set display:none)
95 |
96 |
97 | Smartupdater automatically checks element's size and visibility and stops/restarts itself based on the element's state.
98 |
99 | If you use "Accordion" technique or multi-table view or let users to minimize modules to get more space - consider to activate "smartStop" feature.
100 |
101 |
102 |
103 | In this example minHeight=100px and minWidth=100px.
104 | Resize these modules manually, minimize/maximize them with button, hide/show one or both of them and check network activity.
105 |
39 |
40 |
41 | It is a very basic feature. If data isn't changed, Smartupdater doesn't update client side GUI.
42 | It simply doesn't call callback function.
43 | In this example server returns hours and minutes, so data from server is changed every minute. Because I set poll
44 | interval 5 sec. server returns the same data 11 times. But Smartupdater ignors the data and doesn't update view.
45 | You can clear input field or type something in it to see that date is refreshed 1 time in a minute
46 |
47 |
48 |
45 |
46 |
47 | Sometimes you want to stop polling and continue it later. For example if user minimizes an active object or enlarges another object which covers the active one you don't want to to keep polling as it simply wasting both server and client resources. Smartupdater lets you to stop polling and continue it as needed.
48 |
49 |
50 |