HTML and CSS Reference
In-Depth Information
FIGURE 10.4 A streaming
connection showing tweets that
my server was listening for.
The messages from the server are being delivered as JSON
messages, forwarded on from Twitter's streaming API. So when
they come in, I'll convert the JSON to data and render the tweet
on the screen:
socket.onmessage = function(event) {
var tweetNode = renderTweet(JSON.parse(event.data));
document.getElementById('tweets').appendChild(tweetNode);
};
Now in as many as four lines of JavaScript (excluding the
renderTweet function), I've got streaming real-time tweets
on my page.
TIP The URL that you
use for the Web Socket
does not have to be the same
origin as your document. This
means that you can connect to
servers from third-party services,
which expands the possibilities
of what can be done.
Search WWH ::




Custom Search