Network I/O Is Unpredictable: Node.js

We’ve seen the importance of callback method in our previous video tutorials. Now lets see how networked I/O Is unpredictable.


http-module-get-method-nodejs-network-io-unpredictability

Here we request/ping for information from 3 different servers and look at its response time. Each time we send a request, we get different response time depending upon how busy the server is, its bandwidth etc.

JavaScript File
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var http= require("http"),
      urls  = [ "technotip.org",
         "technotip.com",
      "www.capturecaption.com"
       ];
 
for(var i = 0; i < urls.length; i++){
ping( urls[i] );
}
 
function ping( url ){
var start = new Date();
 
http.get({ host: url }, function(res){
console.log("URL :"+url);
console.log("Response Time: "+(new Date() - start)+" ms");
});
}

Here we require http module, which is built into nodejs, and store it inside a local object called http. We also declare and initialize an array with 3 domain names. Using for loop, we loop through each URL present in the urls array and pass it to a method called ping();

Inside ping method, we record client system date in a variable before sending a request to the server(via URL). Now using http objects get method we send request to the server and see it’s response time. get method takes 2 parameter, first parameter is an object which contains host information – the host url { host: url }. Second parameter is a callback method which automatically gets an object which is returned by first parameter {host: url}. Inside the callback method, we subtract the new system date with the one we recorded before requesting for a response, this way we calculate the response time of each URL.

HTTP get method: Node.js


[youtube https://www.youtube.com/watch?v=vktKMtWoPbA]

YouTube Link: https://www.youtube.com/watch?v=vktKMtWoPbA [Watch the Video In Full Screen.]



output
C:\node>node app.js
URL :technotip.com
Response Time: 1462 ms
URL :www.capturecaption.com
Response Time: 1993 ms
URL :technotip.org
Response Time: 2004 ms

C:\node>node app.js
URL :technotip.com
Response Time: 1381 ms
URL :www.capturecaption.com
Response Time: 1702 ms
URL :technotip.org
Response Time: 1871 ms

C:\node>node app.js
URL :technotip.com
Response Time: 1409 ms
URL :www.capturecaption.com
Response Time: 1628 ms
URL :technotip.org
Response Time: 2001 ms

C:\node>node app.js
URL :technotip.com
Response Time: 1512 ms
URL :www.capturecaption.com
Response Time: 1534 ms
URL :technotip.org
Response Time: 1899 ms

Each time we execute the script, we get different response time, and the order of URLs may also differ, as we can’t predict which server will respond first.

ObjectId ( _id ) as Primary Key: MongoDB

In this video tutorial we shall learn more about ObjectId( _id ) of MongoDB document.

ObjectId is a primary key in any mongoDB document.
We can’t change document id once it has been created and inserted.
If we do not explicitly assign value to _id, then it’ll be automatically assigned and inserted.
We can’t have same document id for more than 1 document.

These ObjectId’s are of BSON datatype.
It’s a 12-byte value.

Basically, each _id is a combination of ..
– Time the document/record was saved/inserted
– Host name of the machine/server it’s running on
– Process id of the server process
– and a random incremental number

Example documents

mongoDB-collection-document

Since the ObjectId‘s are formed taking time of insertion of document into consideration, we can extract this time using getTimestamp() method.

1
db.info.find()[0]._id.getTimestamp()

Custom _id values
We can explicitly define the _id value and in such case system won’t add it for us.

1
2
3
4
5
db.info.insert({
   _id    : 2,
   name   : "Google",
   product: "Google Glass"
})

output

1
2
3
4
5
6
>db.info.find().forEach(printjson)
{
"_id"    : 2,
"name"   : "Google",
"product": "Google Glass"
}

If you insert ObjectId yourselves, then having a separate ‘document_created_at’ field becomes necessary depending on your application requirements.

For that, you can use a inbuilt method Date()

1
2
> new Date()
ISODate("2013-05-01T07:55:12.467Z")

Date is actually a class, but it calls upon the constructor method, hence returning current system/server timestamp, in ISO format.

Remove / Delete / Drop Document

1
db.info.remove({ _id: 2 })

To remove a document, simply pass in a unique { key: value } pair and the document gets deleted/removed.

Primary Key in MongoDB: ObjectId


[youtube https://www.youtube.com/watch?v=YCZV5N01Oqs]

YouTube Link: https://www.youtube.com/watch?v=YCZV5N01Oqs [Watch the Video In Full Screen.]



Navigation of Documents
To navigate between documents, use the index numbers, and to select particular field use it’s key name.

To get the _id of first document

1
db.info.find()[0]._id

0 is the index of first document or record. _id is the field/key name.