Search This Blog

Showing posts with label Network. Show all posts
Showing posts with label Network. Show all posts

Thursday, 25 October 2018

Why Node.js® - several reasons of nodejs popularity?


Node.js®, known as Node is gaining attention of designer. Node has been proved as good option to write highly scalable network solutions. We recently choose Nodejs as our server language.

Asynchronous event driven JavaScript runtime 


Node is designed to build scalable network applications. Using nodejs many concurrent connection can be handle. On each connection the callback is fired, but if there is no work to be done, Node will sleep. This is opposite to common concurrency model where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use.

Node are free from worries of dead-locking the process


Users of Node are free from worries of dead-locking the process, since there are no locks. Almost no function in Node directly performs I/O, so the process never blocks.
Because nothing blocks, scalable systems are very reasonable to develop in Node.

Similarity with Ruby's Event machine and Python's twisted 


Node is similar in design and influenced by system like Ruby's Event machine and Python's twisted. Node took the event model a bit further. It presented an event loop as a runtime construct instead of as a library. In other systems there is always a blocking call to start the event-loop. This behavior is defined through callbacks at the beginning of a script and at the end starts a server through a blocking call like EventMachine::run().

In Node there is no such start-the-event-loop call. Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform. This behavior is like browser JavaScript — the event loop is hidden from the user


HTTP's the most important aspect of Nodejs


HTTP is a first class citizen in Node, designed with streaming and low latency in mind. This makes Node well suited for the foundation of a web library or framework.

Note - Just because Node is designed without threads, that doesn't mean one can't take
advantage of multiple core  in environment. Child processes can be spawned by using our child_process.fork() API, and are designed to be easy to communicate with. Built upon that same interface is the cluster module, which allows you to share sockets between processes
to enable load balancing over your cores. 

Sunday, 10 June 2012

Checking network connection and performing operation in Thread

My this post is related to checking network connection  and how to handle if network connection is not available to make our application more responsive. Later on we will learn how to perform time taking operation in separate thread if connection is present.

First of all we two permission if your are checking wifi connection then you need one extra permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If you want to check connection on click of particular button you can  call this function inside click listener of button

public void ahmadNetworkHanlder() {
   ConnectivityManager connMgr = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        // fetch data here as network is availble
    } else {
        // display error dialog to notify user ,connection is not present
    }
}
Now consider case, Network is available. Use one asynchronous task to perform network operation like downloading data

converting InputStream to string

We are very much aware about InputStream. InputStream is to  read to content from file or URL. If we handle file then we used FileInputStream and while reading from a URL, we used InputStream.


InputStream is a readable source of bytes. Once you get an InputStream it's common to decode or convert it into a target data type. For example, if you were downloading image data, you might decode and display it directly Read this post to know how to convert input stream to bitmap See How to convert InputStream to bitmap. But if we want to convert InputStream to string then we need to read it line by line like given code


// Reads an InputStream and converts it to a String.
  public static String convertStreamToString(InputStream is) throws Exception {
 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
 StringBuilder sb = new StringBuilder();
 String line = null;
 while ((line = reader.readLine()) != null) {
     sb.append(line);
  }
       is.close();
 return sb.toString();
 }


Android News and source code