Wednesday 30 May 2012

Web workers

JavaScript has an important limitation: all its execution process remains inside a unique thread.
This JavaScript limitation implies that a long-running process will freeze the main window. We often say that we’re blocking the “UI Thread”. This is the main thread in charge of handling all the visual elements and associated tasks: drawing, refreshing, user inputs events, etc.
When executing scripts in an HTML page, the page becomes unresponsive until the script is finished.

We all know the bad consequences of overloading this thread: the page freezes and the user can’t interact with your application any more. The user experience is then, of course, very unpleasant.
To avoid the above issues and to provide user with a pleasant experience we can use Web workers.

What is Web workers?
A web worker is a JavaScript that runs in the background, without affecting the performance of the page.  ( You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background. )

The web workers are background workers that run scripts in parallel to their main page. The Web Worker itself is independent of any user interface scripts.

There are two types of web workers available: Dedicated Workers and Shared Workers.

Dedicated workers are linked to their creator. They are also a bit simpler to create and work with than    shared workers.

Shared workers allow any script from the same origin/domain to obtain a reference to the worker and communicate with it.

Dedicated web workers are supported in Firefox 3.5+, Chrome 5+, and Safari 5+.
Shared web workers are supported in Chrome 5.0+ and Safari 5.0+.


Dedicated web workers
To create a dedicated web worker, you simply create a new instance of the Worker object passing in a string that specifies the location of the JavaScript file for the worker code. The following is an example of creating a dedicated worker instance:

var aWorker = new Worker("dedicated.js");

Shared Web workers
Shared web workers provide a way for any window, from the same origin/domain, to share the use of a worker thread.

var aSharedWorker = new SharedWorker("Shared.js");

The first thing to do in order to use the Web Workers API is to create the worker and pass it an execution script URL. Here is an example for that.

var worker = new Worker('worker.js');

Web workers interact with the document via message passing. Those messages can be formed with normal strings or JSON objects.

To send a message to the worker, the postMessage method of the worker object is used as shown below
worker.postMessage("Hi");

Webworker communicates with server using the method called onmessage.
    worker.onmessage(event){
     alert(event.data);}

Web workers are supported by all major browsers except IE.

Be aware that the worker will live until you kill it.

Since they aren’t automatically garbage collected, it’s up to you to control their states. And keep in mind that instantiating a worker will cost some memory, and don’t neglect the cold start time. To stop a worker, there are 2 possible solutions:

*from the main calling page by calling the terminate() command.
*from the worker itself via the close() command.


Monday 30 April 2012



A tryst with CSS

Hi All,
Well I am going to share my experience regarding my attempt to create a website.
I was quite enthusiastic as it was my first experience with CSS and  initially I have decided to apply a lot of font colors and style. But when I finally completed the home page I, myself did not like it. Then I have googled about CSS and thanks to my mentors, who helped me in learning the basic things.
I should mention about one of the many terms, which attracted my attention. It is "Clean Designing".

I realized that clean designing means, designing your website in such a way that it should convey the message to the user in just first few seconds. In other word the user should be able to find out what he is looking for.

Design should make user comfortable enough to go through the website , and it should help him in finding his required information but should not divert the attention on unnecessary things.

As we see in common user point of view, if we are visiting any website for the first time we do not spend so much time there if we do not find the desired content immediately.

We should mind few things while creating a website like :

* What exactly the user want from this website.

* How he expects the information to be.

So , in my opinion the visitor should be able to find out the desired information in minutes.

If he feels it is good then he can even re-visit the site.

Few more important points which I have learned.

* In  corporate website there should not be many colors.

* It would be better not to have more than 2 primary colors.

* Content should be legible and short and clear. (Here clear means, it should convey the meaning properly)

* The user should be able to find the required information immediately as soon as he enters into the website.

* Data should be properly divided into sections.

*Related things should be grouped in using tabs etc.
For example, if it is a site regarding Indian tourism, the visitor expects the content should be presented region wise.

* The color theme also needs to match with the purpose of the site.
For example if the site is all about nature and wild life, then the color should not be red, it would be better to use green color.

* Important thing is, we should design the site in a such a way that we should force/guide the visitor to look or go through a particular section which we want(we can persuade the user by applying different colors and design to the particular section).