Sunday, February 17, 2013

Event driven, asynchronous callbacks and Node.js

Node's approach of code execution isn't unique, but back end execution model is different from environments like PHP or Java.

Node.js core provides methods to reading content of a file in the file-system. fs module provides variety of file system methods.The easiest way to read the entire contents of a file is as follows.
fs = require('fs');
fs.readFile('/home/udara/docs/users.txt');
console.log("Hello Users !!");
Let's assume that the time taken to read users.txt is really long, JavaScript interpreter of Node.js first has to complete reading the users.txt file, and then only it execute the console.log() function. Same as in PHP if this code would be part of a webpage, the user have to wait until it read whole file at once and load the page.

Unlike PHP, Node.js there is only one single process. If there is something happen like above example this will effect the whole process and each and every user would be affected. But in PHP only user who request that web page feel slow page load, but other users requesting other pages would not be affected. 

So where is asynchronous of Node.js ????

fs = require('fs');
fs.readFile('/home/udara/docs/users.txt', function (error,data) {
console.log(data);
});
console.log("Hello Users !!");
Here, without expecting fs.readFile() to directly return a file content to us we pass second parameter an anonymous function.Now Node.js can handle fileread request asynchronously.Only when fileread is done then it will execute anonymous function that was passed to fs.readFile().

Without waiting for the fileread output system will out put Hello Users !! to the console.

 

Monday, February 11, 2013

How to move apps from phone memory to SD card in android

You may find many apps on google play which can use to move apps from phone memory to SD card, but for this to work developers need to enable this option in their apps.Following method is valuable if you do have a phone with small internal memory plus loads of apps which you cant move to SD card using apps like APP 2 SD.

  • Download latest version of Android SDK.
  • Connect the phone using USB cable and enable USB debugging mode.Settings -> Applications -> Development and enable USB debugging
  • Open terminal and navigate to android SDK directory, in my instance cd /home/udara/private/android-sdk-linux/platform-tools/
  • Add that directory to path using, export PATH=$PATH:/home/udara/private/android-sdk-linux/platform-tools/
  • Now by typing adb devices command you will get something similar to,

  • Type adb shell and then run command pm setInstallLocation 2.
  • Now on your phone go to Settings->Applications->Manage Applications and you will find most of the applications are now available to move from phone memory to SD card.

I have tested this on a rooted huawei U8160 running android 2.2(froyo) using UBUNTU 12.04. 

Tuesday, February 5, 2013

browscap to JS navigator

Browscap facilitate to identify what a visitor's browser is capable of, an improved version of native php function get_browser(). You need to update browscap.ini which resides on your server periodically in order to get the correct browser capabilities.

In the recent past with rise of the JS world, you can detect HTML5, CSS3 browser capabilities with the use of JS libraries like modernizr or even with simple JS navigator.

browscap output,


JS navigator output,