 Okay, so if you have seen the speaker video, I think I have said like no PPTs. So apparently they needed some PPTs to show it as part of the video. So just like put together some PPT and most of it is going to be just like link to code or some code samples, some code snippets in there. Okay. So I'm Sudar. I work as a research engineer in Yahoo Labs. Okay, so I've been dealing with web applications throughout my career and currently doing something on Node.js as part of my day job as well. Okay, so that's it and this is what the agenda is going to be. I'll just like be quickly running through the slides and then we'll get to the code and we'll see how we can use different modules in Node.js and things like that. Okay, so I kind of assume that most of you would be slightly new to Node.js but I think now I mean since most of you are already used to it I think I can just like pass in or quickly like go through all these slides. So as you know it's basically it's going to be an evented non-blocking I.O. built on V8 the usual stuff and it's a JavaScript programming environment support C++ add-ons and all that. So was Node.js the first evented system available? No. So there were a couple of other things which were available as an evented system even before Node.js. Some of them was like the twisted framework from Python or the evented event mission from Ruby and all that stuff. So the main difference between all these other frameworks and Node.js is that Node.js provides this event I.O. as a language construct rather than as a library or as a framework. So all these other things provided more as a framework and you might have to probably use it as a library but it's not available as part of the language construct itself and that's where Node.js is different from other evented I.O. things which are present. By the way I assume that all of you know about evented I.O. So anybody here have need to need me to explain about evented I.O. So I kind of assume that all of you know about it. So again Node.js is not Ruby on Rails or it's not Danjo or it's not code lignite. It's a framework which provides you where you can write your own Ruby on Rails or your own Danjo or your own code lignite for that instance and the community is doing that. And in Node.js you have a lot of these frameworks available. The famous among them is Express and Socket I.O. Connect and all that stuff. So this is just a small slide which I put in to explain what is evented I.O. I'm just going to quickly run through it assuming that all of you know about evented I.O. So the basic things right I mean if in any traditional programming language whatever statements you have that gets executed in sequentially. So if a particular statement is going to get blocked for some reason. Maybe it's waiting for some I.O. Maybe it's like waiting for a DB call and it's waiting for a network call and things like that. The next statement is not going to get executed immediately. It's going to wait till that particular statement finishes its execution and the next statement will execute only after that. So this is a typical code flow which you would have probably seen in most of the web applications or any application which involves network calls. So the problem with this is when your execution stack gets stopped at that point and it's waiting for the other part so basically it's going to wait for the input or the output which is going to happen. And generally when you in any computer architecture the input and output is way slower than the execution which is going to happen. So to cope up with this most of the network programs what they do is like they spawn new threads for each of these requests almost all of these threads would be blocked on the IO at some particular point in time. So what's the problem with that? The main problem with that is threads are not lightweight or the resource part. So if you have to create a new thread or a new process the OS has to get involved and it has to create the thread it has to allocate the memory for it and it has to maintain the descriptor for it and all that stuff which takes time and which also takes up a lot of resources. And also there should be a master thread which should maintain all these different threads and things like that. And that's where Node.js is slightly different or Node.js or any event touch system is slightly different there. So what you're going to do is you're going to give a call back to any function which is going to get delayed whether it's going to wait for an input or an output. You're just going to give a call back to it and then you continue your execution stack. And whenever that particular input or output returns back the call back gets called. So this is a typical code flow for that. So here the call back function which is given as a second argument to that method call or whatever would get executed when that IO returns and when initially the code is getting executed the statement after this would get executed would get executed immediately. So what is the main advantage of this? The main advantage is that you're not going to get blocked, you're not going to create multiple threads. What you're going to do is you're just going to have a function call. So for a function call you just basically have to create a heap so you don't have to do anything else. The OS doesn't need to create a new thread or doesn't have to spawn a new process and all that. So it's just going to have some heap allocation done and then the particular code can just keep executing. So that's one of the main advantages of an event at IO. The advantage gets really imminent if you're going to do a lot of concurrent client request models. So basically where if you have a network program which has a lot of concurrent clients connecting to it then this becomes a real advantage for you because you're not going to get blocked by a single client's request. The main program which is going to receive or which is going to consume all these requests can just listen for these requests get it and allocate a call back to it and then can end the execution and then it can go and then probably serve the other clients. So your client queue is not blocked because of input by another process. So any questions so far or am I going too fast or something? So hope everybody is clear about event IO and why we need event IO and all that stuff. So let's see how a call back works now. Sorry can you be a bit loud? Sorry can you be a bit loud I'm not able to hear it all. Yes Yes. So I didn't get the last part. How do you So the question is that since the event at model is going to be slightly different than the traditional model how are you going to do locking right? That's the question. So in Node.js there is no concept of locking. So you don't do any locking here. So the locking comes into picture when there are multiple threats which are going to try to access a single resource so here that scenario is not going to happen so what you are doing is like every request which comes in gets a call back assigned to it. So there aren't any concurrent things which are happening so you don't necessarily need a lock for it. So did I answer your question? Okay. Okay. So the question is if you have two operations which needs to be done so and these operations have to be done sequentially and more than sequentially let's assume that they are dependent on each other so the second operation is dependent on the first operation. So in that case what you have to do is the second operation should start only after the first. So you will have the callback for the first operation and inside the callback for the first operation you can start the second operation. You can assign the callback inside that. So that would solve the dependence issue but generally you don't do that but unless and until they are really dependent on each other and there is no point in proceeding with the second option if the first task is not. So in that case you can put it inside the callback of the first. So your question is like do you have to rearchitect your program. So basically you have to go get into event at IO if you want to do things in Node.js you cannot continue to do it the sequential way. So it also depends on what you actually want to do with your program. So Node.js is good but it's not the solution for all the problems. So there are certain types of problems for which event at IO could be an advantage for you but there could be other if it's just a batch job where you just want to execute a sequence step of jobs and it has nothing to do with so in those cases it may be better you don't do it in the evented way and then you continue to do it in the typical sequential way which you do. So it again depends on what problem you're trying to solve. So Node.js is good but it's not the solution for all problems. So there are set of problems for which Node.js is good there are set of other problems for which the traditional sequential programming model is good. So we might probably have to decide that before you take it out. So any questions before? Okay, so let's see an example of a callback. Okay, so is the text visible or do we want this is good? Okay. So this is just an example for callback and to just explain the concept that things don't happen sequentially. So what we do is we have a set timeout function. You can compare it to a timer or if you're familiar with Java then you can compare it with the threads you create in Java. And it's not technically a thread but I'm just like giving a vague comparison. And so what happens here is so you say that after 2 seconds so this is like 200 milliseconds 2000 milliseconds which is like 2 seconds. After 2 seconds call this function. So this is the function so this is the function which is going to get called after 2 seconds. And the callback is assigned to it and then the execution is going to continue with the next statement after that. So when we execute this you see that first the second statement is going to get executed and then after 2 seconds the callback gets kicked in and then that would get executed. So you can see that. So first the second statement gets executed and after that after 2 seconds the callback gets kicked in and then it gets executed. So, this is how things happen in Node.js, so everything is evented and it is not going to get executed in the order in which it is present, so let us move on, so that was just a small example for evented IO and so you have decided that you are going to use Node.js to, let me tell you how do you do that, so first you have to install Node.js and install NPM, NPM is nothing but a package manager for Node.js, from the Linux world you could probably compare it with an app get or aptitude or yum or something like that, it is a package manager for Node.js and you can use commands like NPM install and then give a module name to install any Node.js modules. And you can also use Node as an interactive console to quickly probably not run commands, so this is similar to what you do in Python, so in Python you have an interactive shell with which you can interact, so the same thing can be done with Node.js as well, so after you installed Node, so in my laptop I have already installed Node, so what you can do is you can just like give Node, just type Node alone and then it opens an interactive command for you, okay, so you can do things like normal assignments, so is it visible or do you mean okay, so you can do things like this or you can test Boolean expressions, something like true, not equal to false and you can also create functions and then call functions and all that, you can import modules, so you can probably try something like V is equal to 3 plus 2 and stuff like that, so it is more like an interactive console where you can, if you are just getting started with Node, you can just like use this, you can try some of these programs and all that, so it can also be used as a calculator or things like that, so basically you can get a feel of Node by using this interactive prompt. And so there are like couple of commands which you can use to get more information, so one is help, so if you say .help, it gives you what are the other commands which are available for you, so you get break, you get clear, so which is to clear the screen and all that, so there are like lot of these commands available, so you can like try it out if you are like very new to Node and then just want to get a feel of it, okay. So the other way to use Node is like creating servers, so Node is very good when you are creating network programs especially server components, okay, so let's see how we can create a server component in Node, so what I am going to do is I am going to just create a HTTP server, so let me, so this is a code for creating an HTTP server which can serve content and things like that, so this is a very simple server, so what I am going to do is every time a client connects to it, I am just going to print the word hello world to it, okay, so this statement here, what we are doing is we are trying to import a module, so this importing a module is similar to what you do in Java, you have import statements in Java and maybe in Python and things like that or hash include in C, so this is similar to that, so you, so this is the common JS format and so what you are going to do is right now I am going to include the HTTP module into this program and HTTP has a method called create server by which I am going to create a server, so that is going to take a callback, see this is the callback function here, so this function would get executed every time whenever a client request is made to the server, okay, so inside the callback what I am doing is just I am going to set the content type for the response as a plain text and then I am going to echo hello world to it and then end it, okay, and then this is going to listen on code number 1332 on the local host and this statement would get executed once the callback has been assigned, so when we execute this you could probably see that this gets executed first and whenever there is a request to the server the callback gets executed, okay, so let us try running this, by the way all these code samples are there in GitHub, so you do not actually have to write them if write them, I would share the link on my ppt where you can get all these code snippets, so I have started the HTTP server now, so it is running on my local host on port 1332, so let us open this from a browser and you can see that the callback gets executed and then you get hello world as a response back, so again the main concept here is that the statements are not executed in order, so the callback gets assigned and the callback gets invoked only when that particular event happens, so this is just a very simple HTTP server but you can probably do a lot of other stuff like if you want to serve static content you can just read it from the disk and then flash it to the response object so that it is served to the client who made the request, you can also set the content type, you can live stream it, do whatever you want, so there are like a lot of modules which help you to do that and so any questions, yes? Okay, so the question is how this particular small server helps when they are going to be concurrent request, so in a traditional web server for example if you take Apache, InginX is slightly different but let us assume it is Apache, so what is going to happen in this scenario, so let us assume there are 10 concurrent requests which come to the server, so the Apache server would start creating threats for each of these 10 requests, so what you will end up having is you have a main thread and then 10 different threads which are created to serve 10 different lines, so Apache is like slightly more intelligent in that way because it does not keep creating threats, it tries to reuse threats and all that, so let us, for the argument let us assume that it creates thread for every new request, so here what is going to happen is there is going to be only one thread, there is no threads which are going to get created at all, so what happens is when a request comes it gets this callback assigned to it on the same process, it is not that you are having multiple processes created for it, it is just that, it is like a function call, so when you do a function call it gets allocated in the heap, in your heap memory you get the return address and all that stuff and then you get the function call, so same thing is going to happen here, so it is not going to create multiple threads, it is just a single process which is executing this, sorry it does not maintain actually, it does not maintain different sessions, so what is actually it is for Node.js it is just a function call, just like a callback which gets called up for each of those requests, yes. Okay so it again depends on what application you want to do, so the problem with Apache is that when there are like lot of coherent requests it ends up creating lot of threads which eats up lot of resources and then the load on your server goes up and things like that, but if you want a way by which you want to talk to multiple processes at the same time and things like that, then you might probably have to go with Apache, right, so Node.js has advantage in certain scenarios, it is not as I said it is not the solution for all problems, so did I answer your question, maybe like you can take it off. So any questions, yes, yes, yes, okay go ahead with your question and I will answer that, okay so to answer your question the thread, so one process taking time or one particular request taking time is not going to affect the other processes, sorry not the other request, so because what is going to happen is every request which comes in is going to have its own callback, it may be IO it may not, okay so see blocking, so there are two things, two places where it can get blocked, right, one is you are doing some huge number crunching where at that particular point it is the CPU which gets blocked, you get blocked at CPU level, there are other places where you get blocked at the IO level, you get blocked because you have made a DB query or you have made a network request or you are reading from a local file, so in that case the CPU is not getting blocked, it is just the IO which is getting blocked and because of the IO, CPU is idle, right, in the first scenario where the CPU itself is getting blocked in that case Node.js is not going to help you, Node.js would help you if your CPU is idle but the process gets blocked because of some IO which is happening, so the second case where it is getting blocked by IO Node.js would be, okay, so again it depends on your application, if predominantly you are going to do only number crunching and you are going to do very, very small amount of IO and your bottleneck is not IO, it is just the number crunching part or the CPU part, then it is not recommended for you to go with Node.js, you can probably go with your, no, you can spawn child processes in Node.js, I have code for that a little later, so you can spawn child processes and then do it but that is not the general way or the more efficient way to use Node.js but you can still create child processes, yes, sorry, once you spawn child processes, yes, yeah, so that is why I said Node.js is not going to give you any advantage in those scenarios where Node.js would really help, let us assume a case where you have to serve a lot of static content or you have to serve big files, so what happens in Apaches, you get, Apaches is going to create a lot of threats for you and so that would probably consume more resources on your server and your server load would go up and your server's capacity to serve the number of clients would go down but if you are going to use Node.js for that scenario, with the same server, with the same resources, you would be able to serve more clients, yes, so the OS is not going to get kicked in and the OS is not going to create multiple threats which in turn is not going to consume more resources, yes, so for each function there has to be some memory which has to be reserved, that is what I said, the heap memory has to be allocated for it but when you compare it with the amount of resources that needed for creating new thread, this is slightly less, so you get an advantage there, so it is not that Node.js is like, so, yeah, that can also fill up, so relatively if Apaches is able to handle 1000 clients, Node.js might probably be able to handle 2000 clients or maybe 3000 but eventually Node.js is also going to make, you know, it is also going to run out of memory but the amount, the number of clients, relatively speaking, the number of clients you can access or number of clients you can serve with the same resource is slightly higher, there are benchmarks I can share it with you offline maybe, yes, in Node.js it is a non-blocking call, so you get a call back for that, okay, I have some code samples for that I can show you how it is getting done, yes. It is not actually going to stop it, right, so what happens is, so there is a queue, there is a client queue which is there, so there are like multiple requests from clients which are coming, so there is a queue and this queue is getting handled by the event loop, so every time a client request comes in from the queue, what this is going to do is, it is either, so in Apaches it is just going to create processes for each of those requests, Node.js is just going to do a call back for each of those processes. Yeah, so the only difference is that it is not, yes, okay, so that is why I was slightly asking whether people have an understanding about evented IO and all that, so the call back function, so the main difference is that if you take a threaded example and a call back, so if you are going to use threads, the OS gets kicked in, so the OS has to create a new process, has to spawn a new process, it has to allocate its own program memory and all that and then it also has to allocate its own heap and all that, so which is slightly more resource intensive operation which has to be done, but in case of a call back, it is not going to go to the OS level at all, it has been maintained by the process, I mean the process which is running Node.js itself, so from the OS perspective there are not going to be any more threads which are going to get created, so okay, okay, so let's probably do that then, the concurrent call, okay, yeah, I think that's okay, so let's take it offline, let's see the other examples which I have, okay, so this is moving on, so this is like how we will be using Node.js as a client, okay, so let me quickly show you the example here, so here what I am basically trying to do is I am including the same HTTP module and I am creating a request object, so for the request object I have to specify the host, port, path, method and all that stuff, the typical things which you have to do for creating an HTTP request and then I am assigning a call back again here, so what's going to happen is whenever there's a response which comes back, this call back gets called in and then it's going to, so from the response object I can get the response status code and again in order to even get the data from it or have to use another call back, so this probably explains the dependency scenario which we talked about, right, so first in order to get data from a request object you need the request object first, so the first call back is doing that, so you are getting a request object first, sorry, a response object first and from the response object you are going to assign another call back when the data is available, okay, sorry, I think this should fire it or maybe like I probably missed it, I'll check that out, I think the request on would fire it up, so when you assign a call back I think it also fires the request as part of it, I'm not sure if I'll be able to run this example because I don't have connectivity to the internet right now, so let's, sorry, okay, then let's, so the server is running here and okay, I think let's move on because like I have a lot of other examples to cover and we have very little time, so these are some of the code modules which are available as part of Node.js, you have the processes for handling client processes and things like that, you have the file system module, you have the networking module which has like a lot of classes of modules, sub modules, using which you can create a TCP connection or the HTTP connection and DNS, you can do DNS and all the stuff and then you have the utilities module which also has like a lot of functions which can make use of in your Node.js, so this link here you can follow this link to find out what are the entire list of modules which are available in Node.js, so I think they are close to some 30 modules which are available, these are part of the core and for, if you need more modules then you can use any of the other extensions which are available to you, so these are built-in modules, these are not third-party modules, okay, so the philosophy of Node is that the core part of the library would be, the core part of the framework is very light and if you want to have any extensions or if you want to have additional functionalities that is available as an extension to you and you can find those extensions and then install it using NPM, so let's see some examples for the process module, so somebody asked me about creating child processes, right, so let's see how we can do that, so this is a code sample which shows you how you can create child processes, Node.js is not going to be very efficient in doing this but suppose in case if you have to create child processes you can do this, so there is a child process module which you have to import and then there is a spawn module, sub-module inside that, so that is going to, using that you would be creating a child process, so here what I'm doing is I'm just like using grep, so the first argument is a command which needs to be executed and the second is a list of arguments which you have to send to it, so I'm just like creating a new process which is going to execute grep, okay and sorry, yes, so let's quickly see three minutes more, okay, so it has spawned a new thread and this is the child process ID, okay, so there are other modules like file system, let me see if I can just like show you the source code for the file system modules, so this basically shows you how you can read files, so this is like, so this particular function, the stat function is going to give you more information about a file, so you can find out what is the size of it or what is the date in which created last modified date and all these meta-information which are associated with the file and this is going to tell you how you can read file from the local system, local file system and as you can see all of these have callbacks, okay, so let's quickly, so you can see, so it was, it just like went ahead and then I'm just like dumping the entire object here, so you get all the information about that particular file, the date in which created, modified time and all that size, what is the UID, GID and all that stuff, okay, so you can also check the other files which are there in my GitHub, you have a lot of these examples to try out different modules, so this is a networking module, so you have the HTTP request, HTTP S modules, DNS modules and all that, you can also make HTTP S calls, the secure calls, I'm not going to run these examples now, again you have these utility module where you have, so I've been using console, so console is part of the utility module, that is basically used to output something to the standard output, the standard output which is available, you also have utils, so let me see if I can quickly show you the code, database access you might probably have to use third-party modules, it depends on which database you want to access, whether you want to access MongoDB or CouchDB or MySQL or whatever, so there are like third-party extensions available and then you just like, install them using npm and then you can start using that, sorry, I don't think there are any native things for database, so as I said the core part of node wants to be as simple as possible, so that you can extend it and then there are like third-party things which are available where you can use it, time's up, okay, so just one more minute, so as I said node.js is useful for certain applications, maybe like these are some examples of applications but again before you even you start you might probably have to figure out whether it's like really useful and things like that and these are some of the technologies, npm is the package manager and modules is like a plugin or add-on for it, then there are like a couple of other modules or frameworks which are available, we have Express, Jade, Socket IO I think, Aditya is going to talk about Socket IO a little later and then there's also a talk about Express where we show you how you can create MEZ applications and all that, so these are the links, thank you.