Archive for January, 2012

Asynchronous Refresh

With the pace with which the web standards and technologies have been evolving, periodical page refreshes to fetch new data from the server has become a thing of history for quite a long time now. Everyone would remember times when pages used to refresh every minute or so when following the updates of a football or a cricket match on the Internet. And those of you who were observant enough would have noticed how things don’t work the same way anymore, and about how updates are delivered seamlessly without any signs of page reload.

I have been working on creating a simple Social Chat-Room for a college project assignment, and while in fact most of its coding is already done, I know the code that I have used is ugly and annoying, talking of how the wall of the chat room gets rendered. I have set the frame containing the display wall to refresh every 10 seconds using the following piece of code in the head section of the page.


Easiest thing to do. And ugly too.

And now comes along a real project, which gives me a good reason to start learning. I’ve got to work on developing a “Live Updates” module for this quite-happening football blog, BigFourZa, a blog about the Big Four of the English Premier League. Something which demands me to start learning. Something for which a continuously refreshing page isn’t going to cut it out.

Time for AJAX. Time for jQuery.

What this Asynchronous Refresh is about is making calls to the server-side document and loading its contents to make it available for dynamic rendering on already present elements in the currently loaded document without any whole page refresh as such. Say we have a page, scores.php, which reads entries from a database and displays it on screen. Now when we add new entries into the database, unless we force a page refresh, the new entries are not going to be displayed on the page.

What we can do with jQuery is asynchronously poll the scores.php from another page, say main.php, and display the contents of scores.php in it. The code of asynchronous refreshing is what is of our interest now, and so the main.php would be something like this:


.
.



.
.

The interval is set to 10000 msec, so every 10 seconds the function is called and scores.php is loaded inside the element <div> with id=myscores, of the currently loaded page, which is main.php. And that’s all it takes. Not to forget to include the source of the jquery.js in main.php. And anyhow, one flaw with this code is that it takes initially 10 seconds before the document is loaded for the first time. For this, you’d have to add this into the head section:


Now to debate, there are much better methods to achieve this than polling, but in terms of simplicity nothing beats polling.

If someone doesn’t have much of any problem with understanding the Document-Object-Model (DOM) of an HTML webpage, then the documentation that is available on the Internet for jQuery is alone sufficient to start playing with it. And for me, it really is time to get working with things. Maybe soon, I’ll be ready with a working prototype of the Live Updates, or with the updated no-shabby-code Chat Room for you all to see.

Comment

Personal Home Page

It’s been a while since I looked back into my blog, holding good with how I thought it would turn out to be. And it’s sooner than I anticipated, which is bad. But then I’ve come to a milestone here that’s encouraging me to get back to writing. 1000 hits to my site. Talking about blogs that get such a response each day, this isn’t anything to brag about. But it sure feels great.

A counter plugin that’s been installed is doing the job. And then there’s my own script that’s running everytime a page on this site is loaded. A simple PHP script. Implementing a counter is possibly one of the simplest things you could achieve with it, but its significance is not to be played down for the reason that the very existence of this language arised merely from the need for keeping visitor count. A 25 year old Rasmus Lerdorf, back in 1994, wanted to track the visitor hits to his personal website, and put together a set of Perl scripts and formed what he called as Personal Home Page Tools. And over the following years, the language was open-sourced which led to the rapid development and evolution of the scripting language what we now know as the PHP: Hypertext Processor.

Bearing a close resemblance to C++, anybody who’s learnt programming at school should be able to pick it up in no time. And the best of all is what you could actually do with it being a web-scripting object-oriented language. For how I began using it to just scrape data off the web, things in it turned out to be so elegant that it drew my interest and now I’ve picked it up quite enough to be doing all of what I do on it.

This language now, means a lot to me.

Comment
Copyright © Bits and Bytes | Arjun Abhynav. Powered by WordPress.