HTML and CSS Reference
In-Depth Information
Listing 13.42 Typical JSON response from server
{
"chatMessage": [{
"id": "38912",
"from": "chris",
"to": "",
"body": "Some text ...",
"sent_at": "2010-02-21T21:23:43.687Z"
}, {
"id": "38913",
"from": "lebowski",
"to": "",
"body": "More text ...",
"sent_at": "2010-02-21T21:23:47.970Z"
}],
"stock": { /* ... */ },
/* ... */
}
Observers could possibly be interested in new stock prices, so they would
show their interest through client.observe("stock", fn); Others may
be more interested in the chat messages coming through. I'm not sure what kind
of site would provide both stock tickers and real-time chat on the same page, but
surely in this crazy Web 2.0 day and age, such a site exists. The point being, the data
from the server may be of a diverse nature because a single connection is used for
all streaming needs.
The client will provide a consistent interface by doing two things. First, it
allows observers to observe a single topic rather than the entire feed. Second, it will
call each observer once per message on that topic. This means that in the above
example, observers to the “chatMessage” topic will be called twice, once for each
chat message.
The client interface will look and behave exactly like the observables developed
in Chapter 11, The Observer Pattern. This way code using the client does not need
to be aware of the fact that data is fetched from and sent to a server. Furthermore,
having two identical interfaces means that we can use a regular observable in
tests for code using the client without having to stub XMLHttpRequest to avoid
going to the server in tests.
 
Search WWH ::




Custom Search