 So the idea is just to understand the interesting things about Node.js and before that any Node.js Okay, at least one way. So how many of you use Node.js? No Node.js. Obviously I'm not Node.js, right? I'm from Wings background, so many tough questions. And you know what I'm going to talk about today. So three concepts. First, understand what Node.js is about. And second thing, some Internet about Node.js and community of Node.js and what is future. Do you see? I think there is a difference. So this is the definition given by the community. Some people say it's given to the idea. So right now I'm not going to explain it. So you see what in my rainbow means in the coming slides. And somebody says it serves as the aspect. And somebody even says it's my personal layer. And what I figured out is, you never try to define Node.js yourself. It's because the same community says all of them are wrong. You cannot define Node.js but instead try to explain Node.js, what Node.js is. So the next few slides, I'm going to explain some programs for how Node.js is created from the now. So Google came up with this thing called P8. So it's basically used to run the JavaScript outside the browser. Chrome uses it internally to run the JavaScript. But you can also use it to run JavaScript outside the browser. And it's very powerful. Typically JavaScript, do you think? It's very slow. But the smart team from Google, they made P8 very powerful. And this is from a guy who introduced JavaScript performance by KDEMs. Can you hear the click? And how do we advance JavaScript? So I have two programs here. Number one, sound.js. So it simply has a function. And it takes two arguments and it sounds like it runs it. And there is other function on the right side, which simply brings a fellow to the console. And we'll see how to run these two sample.js programs using P8. And this is a sample program, not a complete program. So don't try to understand it. Just see the first line. So it says into P8. And so here what I'm doing, I'm just getting the first argument. And getting the file content from the first parameter and then doing something. And so you compile it, and then how do you run it? Node space sound.js. So this is a C++ file, which we created. So I named it as node.cpp. And you compile it so it gets some program. And so this is how I execute this file. Node space sound.js. And sound.js is the first argument, right? So our simple program, take the content from the front.js and compile it. And it prints the output to the screen. So this explains how powerful the V8 is. But we cannot run our print.js here. That's because P8 is just a JavaScript. What it can do? It can create functions. It can do basic JavaScript. JavaScript doesn't print the console. So it's just a browser thing. It's very bad. But we can still do that using V8. So how to do that? Using V8, what we can do is we can call a C function from the JavaScript. For example, we have a printer function. That printer was print, right? So what we can do is we can interface console.log and print function. So they call console.log. And so it actually is combined with the printer function. And it can print what else you move past. But that's, you have to learn that because that's not a scope of this. And so let's see what we have right now. Simple JavaScript program which can print when we interface it. So what we can do is we can add more features to it. There are a lot of C libraries available to pass the HTTP, pylending, TCP, general software. And let's say we somehow make DC plus plus functions work in JavaScript by having the bridge. And once we add all those features, so we'll be having our node.cpp, our executable files. So it's like node.js. We can call it. We have built something like node.js. So we can compare our program with Ruby or Java or Python because typically what it does, so we have runtime engine and we can access files, we can access database and we can do sorted programming. And that's all Ruby, Java and Python. So same in the case for our program. But you'll see what Microsoft is saying. We did this 10 years ago. Can you even guess what they have done? Actives. So it's called JavaScript. So Microsoft they introduced this 10 years ago having JavaScript on the server side and you can even do phylandry. For example, this code. So what it does, it creates an active sorted and creating a folder. It's in fact shipped with all the windows. You don't have to install anything. You just run it using this. So you just run the phylandry.js and you can have something like node.js. So essentially what I'm saying is node.js is nothing new. So having the server side JavaScript has been there. It's in fact even before the JS node. So yeah, so in 10 minutes, in 5 minutes I'll show you how to build something in node.js but it's not as simple as it seems because the guy behind node.js is a Rhyme doll. And the impact spent 6 months to come with the questions. And so he added more sugar to it because what we have right now is only there. It's not just JS. We have a lot of other stuff to add. So obviously to make it popular he had to add a lot of sugar. And what all day? So it's called even at the IO using these keywords. So all of a sudden it's become popular. So how many of you are Rails guys? I recognize this guy from the website. So he's the guy behind Rails. So after building Rails he's become popular all of a sudden. And people call this guy Rhyme doll. And we'll see what we can do with node.js. So I've shown you, right? So it's like a typical Ruby program or Java or whatever. You can build web applications and socket programming. And you can build IDs. In fact pretty much everything. And because it's just a JavaScript so you just have to learn node.js by JavaScript. And this is an interesting slide which performs in it. And learning JavaScript. Typically there are a lot of languages out there. And for language to survive it should be very easy to grasp. For example look at the graph right. So initially what happens people are struggling to learn and as time goes they are not able to learn at all. So they are not going to tell what the solution is. But yeah, so to make a language popular it will be very simple. And what is worse is better. How many of you have heard this concept? Worse is better. Worse is better. So it's a study some 10 years ago and what it tries to say is how the software you build is accepted by the community. You add a lot of features. So what it's trying to say is in order for software to be successful it should be very simple. And being simple is the worst part. And having less features is the worst part. And if it's simple people tend to adopt to it quickly because people have a lot of modules or people have a lot of actions people contribute a lot and that's when the software is very popular. So right now learning Node.js is nothing because it's simply JavaScript but instead what you should learn is it's a modules that's built on top of Node.js. So in Java we have J2V all those modules and similarly Ruby we have a lot of frameworks so it's a framework we have to learn. It's good to learn the internals and that's what I'm going to talk about. So two concepts I'm going to talk about there needs to be a sequence and just think as I hope. Basically these two concepts made the Node.js So this is a sample program which explains what even the program is. So the first line what I'm doing is I'm deleting a file fs.unlink so I'm passing the file to the deleted. So what is the second argument is the callback. So what it does is typically what we do is fs.delete of some file and we forget it. So if at all the next string executes it's like after the file is getting deleted the next string is getting executed but in the event at work what happens is everything is postponed into the queue. For example in this case if you put fs.unlink delete of some some string what happens is it will post the task into the queue and once that is done after delete is getting executed This is another example to get a record from the database what I'm doing is usually I find my name of John passing the name I want a record that's how you do and typically what we do we assign the return value to a local variable but here instead what we are doing is we are passing a second argument called printUser so what happens is when user.findByName completes or when it gets the record it calls this function and inside what I'm doing I'm doing the same name and note that even console.log is non-blocking meaning for i equal to 1 to 1000 you do console.log of some big string the entire for loop will get executed but still the console.log will be right into the string and we will see what we get by putting everything into event at work so this is a sample function whatever all the first two lines and essentially what it does is I'm creating a web server when you hit the URL with the slash what we are doing is we are setting the hello world to the browser and this line is inside the set time out so when I hit local post colon 3000 what happens I'll get 3000 after two seconds because this line is present inside set time out that's okay but what's interesting is let's say 1000 requests come in at a time you do a bit of string and you fire up 1000 queries and how long do you think to complete the entire 1000 request in case yeah so assume that I'm not talking about the client side so let's say talk about server side forget about all the latency all those stuff so a single request is going to take two seconds but I am finally hitting 1000 requests so it's again two seconds if not 2.5 or something how it works is so I am doing set time out of something it's like sleeping so a request comes in and obviously this function will get called and inside what I am doing is I won't sleep for two seconds to call hello world I will post the task to a queue I will delegate the task to somebody and ask him that after two seconds send hello world to the client and then immediately I will look for the other request so 1000 requests come in I am writing all the request asking it, delegating it they will take each and there will be 100 events they will be running thoroughly and each and every event will send the hello world to the client simultaneously and that's the power event loop but don't think that without event loop it's going to take 2000 seconds but with the help of event loop I made it two seconds I don't have a quick question you said it will return simultaneously what was the first thing it's an event loop right, it's a queue it can't be simultaneous so JavaScript is a single product JavaScript is a single product so how it is concurrency can talk about it there are slides coming it talks about how internally concurrency is achieved so I will talk about it most of the concepts, no JS concepts you have to think only when concurrency matters when you have a web app when you get maybe 5 requests per second you don't have to worry because anyway you are going to serve a request in one or two seconds per user and you really don't have to worry about how to save all those stuff but only when concurrency matters think about these concepts event or programming I will come back to your question but to understand if you look at the same program instead of set time mode what other option I have typically how do you do it in Java or Ruby or any language because ATV don't support event or system so what else can you do to achieve the same thing spawn or thread so what I can do is I can run this line inside another thread spawn a different thread okay different process you can still achieve the concurrency with the current concepts with all the languages but what is the difference between achieving the same thing with thread or with event or system and this program might help you to understand it so here I have two functions called dump the first version it's printing IE under times but that drop is inside the thread but javascript doesn't update so this is some hard bit statement which I put and you can think in terms of any other language here I am doing the print of IE in the event loop okay so both of them are going to run thoroughly and it's going to print IE under times but what is the difference why event programming is popular and you can see it first problem what happen in threading is whenever a new thread is created all the local variables that's available at the hope point will be available to the threading block so you have 100 threads running each and every thread will have its own local copy of all the local variables it's in stack so you have infinite variables in the dump function declared there's too much memory but in case of event loop the local variables from dump is not cloned into this event block so all I am doing is I am trying to save the memory okay so that's one problem so in the event loop we are trying to reduce the memory so the process which event loop takes less memory and there is another problem with threading so what happens is each and every thread will have infinite copies of local variables so whenever context switching in threading what happens so actually the process switches from one thread to another thread and at that time what happens it's going to pull in all the local variables from the stack and then spawn a new thread and that context switching is heavy but here event loop that's not the case because there is no context switch but I will conduct it later as you have seen it simply calls another function that is what event does and so another problem is whenever a thread completes it has to remember what is the next line to get executed so in this case let's say the thread ends it has to come to the next line so it's called thread stack so it has to remember where it should go when the thread is completed but in case of events you don't have to remember where to return because events is something like when you are done deleting with a file call the event so the event will end immediately after calling the callback so it doesn't have to return to the call function so this is another example so it's a benchmark between a fashion engineer it's really popular comparison in the x axis what we have is number of connections meaning we have a single Apache process a single Node.js enginex process we keep on using Apache and enginex and as the number of requests per second increases in the y axis what you see is the memory consumed by the process so you can see that Apache is taking more memory but enginex it stays flat so throughout the process enginex is taking less memory and that's one thing that's cool with even that programming but with good there comes a lot of bad parts so this is an example see I have a function called inside what I'm doing is I'm simply returning line 7 and 8 what I'm doing is I'm creating IO and home incidents so what happens is two timeouts we get created but when we do this this is the error you will get because inside this function what you are doing is this is the error throw you off error at sample.js line 4 sample.js in line 4 we have an error but which cause the error is it line 7 or line 8 because the stack trace doesn't have it the reason is set timeout what it does is it simply creates a queue and it puts it just creates a heap and it says call this function after 5 seconds or 10 seconds and that even though it doesn't keep track of all the stack trace and all that that stack and that's one problem with unit program debugging is retained and this is another program and try to understand any of you about the program I'm sure you won't get it because two reasons one is the program is complex and second thing is the program is complex making the font size and so in the synchronous world this is how you do it you see what I'm doing so inside I have this called capless so assume that cap is going to return the capless version of what you are passing and again you are calling a bold and again you are trying to bring console.log if any exception happens you see this is in the synchronous world everything looks good for the same program when I try to do it in a synchronous world it's becoming complex the reason is so if you look in here what I'm doing I have a function called capless which accepts a text that text is going to capless so once I have caplessed this I will call this function because the capless function is asynchronous it's not going to return you the capless version immediately so the only way is pass another function to capless so what will happen when the capless is done with this process it's going to call this function and and the result is stored in the result one so now I have caplessed version and without what I'm going to do I have to bold it so what I'm doing again calling this bold function I am passing the capless version so again it's not synchronous because it will return the bolder version immediately so what I have to do create another callback and so this function will get executed when bold is done and I will have the final version in the result two but you still can solve this problem because there are a lot of other solutions available for example what you can do is you can use this library it's available you just search for Node.js add-ins you will get all those functions another problem is with accept and handle so here if you see it what I'm doing is I'm just deleting a file and if the file there is any exception I'm actually saying console.log unable to delete but it's bad don't do this anyways why? because the error also would be asynchronous even the error would be asynchronous even if there is an error the system won't return it immediately to the callback yeah so yeah correct the reason is so try catch what I'm trying to do here is I'm just putting something in the event event group and the catch I'm catching whatever error happens while putting in the event group but inside the event group something happens this won't work so what we have to do is the callback typically will return you the error and inside this you have to handle all the so that's another problem with and so now that we have learnt what is event-driven we'll see what is asynchronous okay so in Node.js it's a single thread application because JavaScript it's a single thread right so it cannot run two things fairly okay so to solve that what I consider is let's say we have under events in the event group under events in the event group and it looks like events are being processed but it's not the case so let's say you show the function right set time out of do something but if I put sleep there what happens is you won't get the entire result in two seconds because sleep actually blocks it so in Node.js what happens is there is always only one event group that's running meaning first one event it'll process once it's done it'll move on to the next event so when you have such a restricted system the event a single event cannot actually do complex stuff it cannot actually do IO all the stuff because if it blocks all the other events also is getting blocked right so what we need is we need to have a mechanism where the call which you are trying to make should behave like asynchronous for example let's say you delete a file let's say it's happening in an event instead of you waiting till the file delays won't it be good if the operating system notifies you when the file is deleted so so to answer your question and it's the operating system that runs its own press that will actually notify you when the event is done and if all KQ select all of these things so to handle asynchronous IO these are the system where it calls so using these things asynchronous is main thing but the problem is there are still certain function calls which doesn't do proper asynchronous for example let's say you have to watch if a file is modified or not okay it still has its own thread there is no proper mechanism for you to modify when the file is modified so there is separate thread internally that's going on that will keep hold for whether the file is modified or not and that will know it for you you will have questions if you don't have questions yeah any questions suppose to end at level 45 right this what I found is before choosing a plugin or module you have to really differentiate what the module does I found my SQL plugin which is not doing things asynchronously instead what it's doing is it's again creating its own fork and then does so when choosing a plugin try to understand whether it's really doing things asynchronously so now that we know how to do concurrency in too many ways one way which we have seen using events and sometimes events are not possible for example the watch file there is no way for the operating system or something else to modify you when the asynchronously so that time we have to use threads and how to use threads we know JavaScript doesn't no JS you cannot use threads right so what you can do is you can create a c c function that does threading and then you can expose the interface via JavaScript and then can be achieved even with no JS or simply you can fork a new process no JS allows you to do that you can fork a new process and then fire the cpu concurrency task I can even have a separate process typically web applications what happen it will listen to some particular port area or something and but the problem is the no JS process cannot run on multi-core meaning you can't use only one process so how to achieve full use of the cpu you can have n processes right and even load balance so a request comes to attach and or some other load balancing server can load balance too as many problems so you can solve the the multi-core let's say you are setting up something like that I have a system that lets say 8 cores and then spawn 8 processes to handle this now let's say if I have some kind of information that has to be shared across some kind of shared state like session and all how do you go around it how do you share information across instances typically web applications are stateless they should be stateless most people wouldn't write and there are people who wouldn't write so if they have to exchange information what is the best possible solution I will go over it in the next point so what he is saying is a request comes in it goes to one process so it completes it and similarly another request comes in it will go to other process but that's not possible always because sometimes that two process has to communicate each other for example IOC channels so lot of people will try to communicate to one process so IOC is like lot of people chatting so all the request has to be routed to a single process but you cannot do that but that's when these web workers all those stuff come in what you do is there is something called domain softwares in Unix using the domain softwares N processors can listen on a single port so let's say you have 10 processors and all the roles are listening at port 80 but ideally you cannot listen at single port using domain softwares you can do it and web workers will actually help you to message passing all the stuff is possible so you don't have to really worry about passing messages and all so it will be easier to take care so this is my last session so Vita Pistri about board JS so it all started in 2009 and so that very big job to work on it I don't know how many of us will do something like that he first presented this in JSCon in fact it was for a slot and finally someone got it and he flew away the entire audience by building an usage server chat server just full on lines of code and that's how more JS was introduced and all of a sudden a lot of people start inviting he goes to yahoo, he goes to microsoft google he goes to google and then he is presenting it everywhere and right now it's very popular in Vita how many of you have used Vita Pistri ok so it's a quick websites of course if you look at the second it's Node.js so it's very popular in Vita and then Microsoft they came forward and then they sponsored the windows port of Node.js it seems to run faster in windows I'm not sure how much money is involved in that anyway that's what line says it seems faster in windows ok so let's talk about web frameworks the modules are available so in Rails I can think of two web frameworks available Ruby word, Rails, Sinatra, Java, J2J but in the case of Node.js there are 40 web frameworks available that's because it's very simple and easy to action and a lot of people have a lot of stuff and database modules and this is a tweet from the line there it says I wish for less hype probably I don't know why it's popular really because of hype then that guy is wishing for less hype and it's confidence I told you that JAs even JAs.NET in the windows JAs these are all alternatives Node.js is the only solution available there are infinite solutions available and they don't even get good traction this is the last line so Node.js is simply like cool things put together so B8 and JavaScript and and and and still it's I don't feel it's like production ready you can build tiny apps to build scale up applications but I really haven't seen any big interface applications build using Node.js and actually the mobile service that's just one and another one is live standard Node.js is the entire hosting platform runs in Node.js as you support it's a little wrong to say that it's not ready maybe it's not it's not ready enough to be competing with Rails but it's still ready enough to be serving some needs the reason I'm saying is I found this code to a buggy meaning you host Node.js up to Nicaragua and largely it crashes so I'm not saying you don't have to use Node.js in the production because as you say the link is using they in fact actually use Rails in fact previously they had tool servers to and increase the load input by twice so Node.js was still used in production but not big interface applications that's what I mean by interface apps big apps meaning hundreds of pages, lot of database calls in fact yeah so any questions who is better than HTML5 workers HTML5 they are not one of the same right so HTML5 web workers is an API right that's an API ok the same thing you can do in even I mean there's a module for Node.js it's called web worker and it implement the same thing in Node.js so Node.js threading is how Node.js threading is achieved how it differs from web workers ok so Node.js threading you have to talk about only when two events happen by way ok yeah but in Node.js how threading is achieved are they using any descent pattern to achieve the threading you mean you have to at the developer you are talking about you are talking about how do you do it in jQuery also threading is now there threading is achieved from where they use promise pattern yeah so if at all you want to use threading there are something on Node.js and lot of threading modules available that you can use to achieve threading but you don't have to really use threading because javascript is supposed to be single thread my question is you don't use threading you are talking about voice or something I told you no threading is not having use threading no threading in the promise series they use 3 shares for 3 shares one is promise fulfilled or not fulfilled or it's failed or something so you are talking about users and promises it's not just about threading it's about how we are using that so it's possible to share you can have promises the same code that you use so promises and client side javascript can be ported to server side also so my question was it's possible to share a data with root 3rds there is no such thing I don't know