1 00:00:00,000 --> 00:00:08,350 2 00:00:08,350 --> 00:00:09,710 >> KEVIN SCHMID: Hello everybody. 3 00:00:09,710 --> 00:00:12,640 Welcome to the CS50 seminar on Node.js. 4 00:00:12,640 --> 00:00:13,955 My name is Kevin. 5 00:00:13,955 --> 00:00:15,580 I'm a CS50 TF. 6 00:00:15,580 --> 00:00:17,650 And I'm sort of like really excited about this seminar. 7 00:00:17,650 --> 00:00:20,430 I think Node.js is very cool. 8 00:00:20,430 --> 00:00:24,200 I hope that this seminar can be used as a good, I guess, springboard for 9 00:00:24,200 --> 00:00:26,380 some of your final projects if you're interested in using 10 00:00:26,380 --> 00:00:27,630 something like Node.js. 11 00:00:27,630 --> 00:00:29,770 12 00:00:29,770 --> 00:00:33,320 >> We'll sort of start the seminar off by just talking about a little bit of the 13 00:00:33,320 --> 00:00:36,970 kind of background scalability perspectives of Node.js, and then 14 00:00:36,970 --> 00:00:39,240 we'll move to some code examples. 15 00:00:39,240 --> 00:00:42,340 And I'll have the code on a website, and you can look at the code. 16 00:00:42,340 --> 00:00:45,475 And after the seminar, I'll sort of talk about how you can set up Node.js 17 00:00:45,475 --> 00:00:48,220 on your computer. 18 00:00:48,220 --> 00:00:48,710 >> OK. 19 00:00:48,710 --> 00:00:49,760 So let's get started. 20 00:00:49,760 --> 00:00:53,700 So I guess I just want to talk about web servers, really, first. 21 00:00:53,700 --> 00:00:59,730 And to start this discussion, I basically have a diagram which is from 22 00:00:59,730 --> 00:01:04,269 the textbook used for CS61, which basically shows the interaction 23 00:01:04,269 --> 00:01:08,510 between a client process, like your web browser or like your aim client or 24 00:01:08,510 --> 00:01:11,340 something like that, and a web server. 25 00:01:11,340 --> 00:01:15,150 So this kind of looks similar to the picture that you saw in lecture on 26 00:01:15,150 --> 00:01:19,270 Wednesday where basically we have some client process like Google Chrome. 27 00:01:19,270 --> 00:01:22,980 >> And then step one is the client sends a request. 28 00:01:22,980 --> 00:01:27,510 So that can be something like well let's visit, I don't know, CS50.net. 29 00:01:27,510 --> 00:01:29,320 So we issue that request. 30 00:01:29,320 --> 00:01:34,280 And does anybody remember the name of the protocol that specifies how that 31 00:01:34,280 --> 00:01:35,610 request should be structured? 32 00:01:35,610 --> 00:01:36,382 Yep. 33 00:01:36,382 --> 00:01:37,650 >> AUDIENCE: [INAUDIBLE]. 34 00:01:37,650 --> 00:01:38,150 >> KEVIN SCHMID: Exactly. 35 00:01:38,150 --> 00:01:40,100 So it's like HTTP, right? 36 00:01:40,100 --> 00:01:44,720 So basically the specification for how that request should actually be laid 37 00:01:44,720 --> 00:01:47,450 out, because at the end of the day, that request is really just like a 38 00:01:47,450 --> 00:01:50,240 string that basically says I want this. 39 00:01:50,240 --> 00:01:53,580 And the specification for that is HTTP. 40 00:01:53,580 --> 00:01:55,270 So that's like a protocol. 41 00:01:55,270 --> 00:01:57,920 >> So then the server receives that request. 42 00:01:57,920 --> 00:02:01,610 So you guys have a web server installed in the CS50 appliance. 43 00:02:01,610 --> 00:02:02,460 It's Apache. 44 00:02:02,460 --> 00:02:06,230 And this week when you work on problem set seven, you'll actually be working 45 00:02:06,230 --> 00:02:08,160 with that web server. 46 00:02:08,160 --> 00:02:12,380 So the server receives that request, and then it has to kind of scratch its 47 00:02:12,380 --> 00:02:15,090 head and say like well what do I do with this? 48 00:02:15,090 --> 00:02:20,060 >> So based on what it decides to do, then it may have to contact some kind 49 00:02:20,060 --> 00:02:20,730 of resource. 50 00:02:20,730 --> 00:02:23,700 And that resource could be a lot of different things. 51 00:02:23,700 --> 00:02:26,810 For one, it could be just like a static HTML file. 52 00:02:26,810 --> 00:02:29,820 So it could just be like some HTML that is like for 53 00:02:29,820 --> 00:02:31,100 your personal website. 54 00:02:31,100 --> 00:02:35,360 It could be a static file like an image or like a movie that you have. 55 00:02:35,360 --> 00:02:37,660 It could even have to talk to some kind of database 56 00:02:37,660 --> 00:02:39,530 like a MySQL database. 57 00:02:39,530 --> 00:02:43,910 So it doesn't always have to communicate with a resource, but in 58 00:02:43,910 --> 00:02:45,700 some cases, it could. 59 00:02:45,700 --> 00:02:47,800 >> So then what it's going to do after that is it's going to 60 00:02:47,800 --> 00:02:49,430 send back the response. 61 00:02:49,430 --> 00:02:53,130 And the response for this is also specified by HTTP. 62 00:02:53,130 --> 00:02:54,830 So then the client can receive it. 63 00:02:54,830 --> 00:02:56,740 It can tear it apart and process it. 64 00:02:56,740 --> 00:03:00,900 And then you get a web page like Google or CS50.net or 65 00:03:00,900 --> 00:03:02,240 whatever you went to. 66 00:03:02,240 --> 00:03:03,100 OK? 67 00:03:03,100 --> 00:03:06,080 >> So this is the basic interaction that we're going to be dealing with. 68 00:03:06,080 --> 00:03:08,770 And we're pretty much going to be focusing on this part of the 69 00:03:08,770 --> 00:03:10,640 interaction, the server. 70 00:03:10,640 --> 00:03:10,990 OK. 71 00:03:10,990 --> 00:03:12,210 Cool. 72 00:03:12,210 --> 00:03:15,500 Anybody have any questions so far? 73 00:03:15,500 --> 00:03:17,720 OK. 74 00:03:17,720 --> 00:03:22,430 >> So as we said, the web server receives this HTTP request and then issues this 75 00:03:22,430 --> 00:03:24,760 HTTP response. 76 00:03:24,760 --> 00:03:29,100 And like we talked about before, the CS50 appliance web server is Apache. 77 00:03:29,100 --> 00:03:32,490 So when you guys work on P set seven, you're going to be working with the 78 00:03:32,490 --> 00:03:34,120 Apache web server. 79 00:03:34,120 --> 00:03:37,890 You'll never have to really work with Apache directly too much. 80 00:03:37,890 --> 00:03:41,920 You sort of configure Apache a little when you specify the virtual hosts or 81 00:03:41,920 --> 00:03:44,970 the v hosts, and we'll get to that in a little bit. 82 00:03:44,970 --> 00:03:50,620 >> But basically, the Apache web server set up to work with PHP kind 83 00:03:50,620 --> 00:03:51,730 of out of the box. 84 00:03:51,730 --> 00:03:56,170 So what really happens is when you go to one of your websites like, say, 85 00:03:56,170 --> 00:04:00,360 local host slash index.PHP or something, is your browser sends that 86 00:04:00,360 --> 00:04:04,330 request, and then Apache is sitting there and figures out to do with it. 87 00:04:04,330 --> 00:04:08,840 And the action is to execute that code in index.PHP and 88 00:04:08,840 --> 00:04:11,330 then send it off back. 89 00:04:11,330 --> 00:04:15,640 So there's that. 90 00:04:15,640 --> 00:04:16,980 So we sort of talked about this. 91 00:04:16,980 --> 00:04:21,990 So it could just serve a static file or run some PHP code and then issue 92 00:04:21,990 --> 00:04:23,510 the response. 93 00:04:23,510 --> 00:04:27,670 >> So then a common question that can come up is well, how do we really deal 94 00:04:27,670 --> 00:04:31,750 with having multiple users at the same time? 95 00:04:31,750 --> 00:04:36,930 So imagine if you were writing a web server, if you had a web server that 96 00:04:36,930 --> 00:04:39,900 you were trying to write in something like C or something like that, 97 00:04:39,900 --> 00:04:45,150 basically you can think about how there could be some kind of code that 98 00:04:45,150 --> 00:04:49,330 would receive the request, but then it has to do all this work on it. 99 00:04:49,330 --> 00:04:53,060 It may have to, for example, contact the database or something like that. 100 00:04:53,060 --> 00:04:53,300 Right? 101 00:04:53,300 --> 00:04:56,010 And then it would do that kind of processing and then 102 00:04:56,010 --> 00:04:57,060 sent back the response. 103 00:04:57,060 --> 00:04:58,950 So that's like the high level overview. 104 00:04:58,950 --> 00:05:04,210 >> But it's not immediately obvious how you can do that so that two people or 105 00:05:04,210 --> 00:05:09,040 even 1,000 people could work with your web server at the same time. 106 00:05:09,040 --> 00:05:14,880 So the solution that Apache uses is called threads or processes. 107 00:05:14,880 --> 00:05:16,770 So you may have heard of these terms before. 108 00:05:16,770 --> 00:05:22,190 It's OK if you haven't, but just think about threads or processes as ways for 109 00:05:22,190 --> 00:05:26,290 an operating system or a user program or something like that or a web server 110 00:05:26,290 --> 00:05:28,810 to sort of execute multiple things at once. 111 00:05:28,810 --> 00:05:31,760 So you may have heard the term like threads of execution. 112 00:05:31,760 --> 00:05:34,140 So it's kind of like you're sort of multitasking. 113 00:05:34,140 --> 00:05:37,710 >> And if you've seen on the box of your laptop, or something like that, 114 00:05:37,710 --> 00:05:43,040 multicore, what you can do is you can run two different threads on different 115 00:05:43,040 --> 00:05:46,700 parts of the CPU so that they can actually happen at the same time. 116 00:05:46,700 --> 00:05:48,100 So this is really powerful. 117 00:05:48,100 --> 00:05:52,270 And this is kind of Apache's solution to this problem. 118 00:05:52,270 --> 00:05:57,900 >> So are there kind of like any issues with this approach though? 119 00:05:57,900 --> 00:05:59,870 So I guess I kind of wrote them there. 120 00:05:59,870 --> 00:06:03,440 But both of them sort of use a lot of memory. 121 00:06:03,440 --> 00:06:07,490 It's very expensive to create a thread or a process. 122 00:06:07,490 --> 00:06:11,750 >> And part of the reasoning is that just like when you're running a C program 123 00:06:11,750 --> 00:06:15,090 like your main and then that calls another function, that has 124 00:06:15,090 --> 00:06:16,520 some kind of stack. 125 00:06:16,520 --> 00:06:19,910 So threads also require an entirely separate stack which 126 00:06:19,910 --> 00:06:21,220 can be quite large. 127 00:06:21,220 --> 00:06:25,170 And if you can imagine having tons of users on your website, you would have 128 00:06:25,170 --> 00:06:26,280 a lot of different threads. 129 00:06:26,280 --> 00:06:28,230 That's a lot of stacks to manage and maintain. 130 00:06:28,230 --> 00:06:31,280 So it's large memory consumption. 131 00:06:31,280 --> 00:06:35,650 >> And then, also, let's say you only have one CPU, or let's say you have 132 00:06:35,650 --> 00:06:38,460 more threads than you have those multicores. 133 00:06:38,460 --> 00:06:38,730 Right? 134 00:06:38,730 --> 00:06:43,280 So let's say you had 10 threads and you only had five CPUs. 135 00:06:43,280 --> 00:06:46,260 You kind of have to do this thing where you switch between the current 136 00:06:46,260 --> 00:06:49,090 one that's running because you can't run all 10 at once. 137 00:06:49,090 --> 00:06:50,980 And that's called a context switch. 138 00:06:50,980 --> 00:06:54,260 And that term actually has a couple of different contexts, but let's just 139 00:06:54,260 --> 00:06:56,620 think of it as switching between two threads. 140 00:06:56,620 --> 00:06:59,730 That can be pretty expensive because basically what you have to do is you 141 00:06:59,730 --> 00:07:03,340 have to stop what you're doing, save the state of that running thread, and 142 00:07:03,340 --> 00:07:05,440 then switch to somewhere else. 143 00:07:05,440 --> 00:07:09,420 >> So does everybody kind of see the motivation of why threads and 144 00:07:09,420 --> 00:07:12,030 processes might be a little bulky? 145 00:07:12,030 --> 00:07:13,840 And did you have a question? 146 00:07:13,840 --> 00:07:14,376 OK. 147 00:07:14,376 --> 00:07:15,070 Cool. 148 00:07:15,070 --> 00:07:18,090 Anybody have any questions? 149 00:07:18,090 --> 00:07:19,620 OK. 150 00:07:19,620 --> 00:07:26,720 >> So if we take a step back for a second, there's kind of like an 151 00:07:26,720 --> 00:07:30,350 observation that we can make about a lot of web applications. 152 00:07:30,350 --> 00:07:34,810 And that's really that a lot of them actually don't do that much useful 153 00:07:34,810 --> 00:07:37,140 work inside of a thread. 154 00:07:37,140 --> 00:07:41,170 So has anybody started on P set seven at all? 155 00:07:41,170 --> 00:07:45,650 So do you want to maybe describe some of the parts? 156 00:07:45,650 --> 00:07:47,850 Have you worked on login or something like that? 157 00:07:47,850 --> 00:07:49,330 >> AUDIENCE: No. 158 00:07:49,330 --> 00:07:49,780 >> KEVIN SCHMID: OK. 159 00:07:49,780 --> 00:07:50,150 Never mind. 160 00:07:50,150 --> 00:07:50,900 Sorry. 161 00:07:50,900 --> 00:07:55,790 But basically, in the P set, you're going to be making a lot of sort of 162 00:07:55,790 --> 00:07:59,760 queries to a database to get some information from that database. 163 00:07:59,760 --> 00:08:03,330 And what your code is going to be doing, what that Apache process or 164 00:08:03,330 --> 00:08:06,030 that Apache thread is going to be doing while it has to contact the 165 00:08:06,030 --> 00:08:08,990 database is it's sort of going to be sitting there and it's going to be 166 00:08:08,990 --> 00:08:12,130 waiting for the database to reply. 167 00:08:12,130 --> 00:08:16,290 >> Now that might not sound like that big a deal because the database is on your 168 00:08:16,290 --> 00:08:18,240 CS50 appliance, right? 169 00:08:18,240 --> 00:08:22,930 But there is some kind of network latency there because now the web 170 00:08:22,930 --> 00:08:26,830 server has to issue its own request to the database to communicate with the 171 00:08:26,830 --> 00:08:29,520 database and then get that information back. 172 00:08:29,520 --> 00:08:33,190 So now it's like well wait for me, I'm going to go get something from the 173 00:08:33,190 --> 00:08:35,770 database and then there's a lot of waiting going on. 174 00:08:35,770 --> 00:08:36,870 Does that make sense? 175 00:08:36,870 --> 00:08:38,580 >> And for some things it's not that bad. 176 00:08:38,580 --> 00:08:41,950 If it just has to, for example, access memory, that's not like 177 00:08:41,950 --> 00:08:44,100 horrible I/O latency. 178 00:08:44,100 --> 00:08:47,110 And when I say, I/O latency, what I'm referring to is like any kind of like 179 00:08:47,110 --> 00:08:48,290 input output. 180 00:08:48,290 --> 00:08:52,950 But to access a file on the disk, like if I wanted to serve the static HTML 181 00:08:52,950 --> 00:08:57,850 file that was on my web page or something like that, I kind of have to 182 00:08:57,850 --> 00:09:02,310 stop for a bit, read that file in from the disk, and then in 183 00:09:02,310 --> 00:09:04,400 that process I'm waiting. 184 00:09:04,400 --> 00:09:06,700 I'm not doing useful work. 185 00:09:06,700 --> 00:09:11,270 >> This is not true of everything, but it is common in applications like P set 186 00:09:11,270 --> 00:09:13,960 seven and a lot of applications that you're not 187 00:09:13,960 --> 00:09:15,440 actually doing much thinking. 188 00:09:15,440 --> 00:09:19,090 And when I say thinking, I mean like computational work. 189 00:09:19,090 --> 00:09:23,270 So computational work could be something like, say, you wanted to 190 00:09:23,270 --> 00:09:26,590 write a web server that just computed the nth Fibonacci number. 191 00:09:26,590 --> 00:09:29,300 That doesn't sound like a particularly fun web server. 192 00:09:29,300 --> 00:09:34,220 Like I wouldn't expect that site to be the next Facebook, but that is some 193 00:09:34,220 --> 00:09:35,610 kind of computational work. 194 00:09:35,610 --> 00:09:39,570 >> And you can imagine replacing that with some other kind of interesting 195 00:09:39,570 --> 00:09:43,070 computational work. 196 00:09:43,070 --> 00:09:46,050 Let's say you were writing something that calculated the degrees of 197 00:09:46,050 --> 00:09:49,170 separation between two people or something like that. 198 00:09:49,170 --> 00:09:51,860 So that does involve some kind of calculation, right? 199 00:09:51,860 --> 00:09:56,630 And even then, to do that you still have to do a lot of waiting for maybe 200 00:09:56,630 --> 00:09:59,550 you have to query a database to look up who's friends with who or 201 00:09:59,550 --> 00:10:00,600 something like that. 202 00:10:00,600 --> 00:10:03,510 So there is that kind of notion of computational work. 203 00:10:03,510 --> 00:10:05,260 Does that make sense? 204 00:10:05,260 --> 00:10:08,258 Does anybody have any questions? 205 00:10:08,258 --> 00:10:11,960 >> Oh and I guess I put chat servers there because chat servers are kind of 206 00:10:11,960 --> 00:10:13,240 another good example of this. 207 00:10:13,240 --> 00:10:15,250 A chat server doesn't have to do much thinking. 208 00:10:15,250 --> 00:10:18,350 It just has to wait for people to send messages and then when 209 00:10:18,350 --> 00:10:19,800 they do, send them. 210 00:10:19,800 --> 00:10:21,050 OK? 211 00:10:21,050 --> 00:10:23,410 212 00:10:23,410 --> 00:10:28,180 >> So just to recap again, Apache and similar web servers like that fork a 213 00:10:28,180 --> 00:10:31,470 lot of threads and processes which can be kind of wasteful. 214 00:10:31,470 --> 00:10:37,530 So I guess the question that may come from that is do we need to have 215 00:10:37,530 --> 00:10:39,610 multiple threads and processes? 216 00:10:39,610 --> 00:10:41,890 What if we just had one? 217 00:10:41,890 --> 00:10:45,710 >> So let's kind of paint a picture of what this would look like. 218 00:10:45,710 --> 00:10:47,810 So let's use only one thread. 219 00:10:47,810 --> 00:10:48,660 OK? 220 00:10:48,660 --> 00:10:52,790 So just imagine this with one thread. 221 00:10:52,790 --> 00:10:56,600 >> Let's suppose we weren't really doing that much useful-- and when I say 222 00:10:56,600 --> 00:10:59,450 useful, I mean computational work-- 223 00:10:59,450 --> 00:11:01,130 in those multiple threads before. 224 00:11:01,130 --> 00:11:04,180 So let's kind of consolidate everything into one thread. 225 00:11:04,180 --> 00:11:07,780 So what if we had one thread that kind of just goes around in the loop and 226 00:11:07,780 --> 00:11:10,880 constantly checks did something new happen. 227 00:11:10,880 --> 00:11:15,130 So for example, something new happened could mean I got something back from 228 00:11:15,130 --> 00:11:19,310 the database, or somebody sent me a new HTTP request. 229 00:11:19,310 --> 00:11:22,290 So those are kind of events that happen, right? 230 00:11:22,290 --> 00:11:26,130 >> And then what I can do when those new things happen is in this same thread 231 00:11:26,130 --> 00:11:30,120 of execution, this single thread of execution, I can call some code that 232 00:11:30,120 --> 00:11:32,410 would handle that particular thing. 233 00:11:32,410 --> 00:11:36,640 So for example, if I got something back from the database, I could run my 234 00:11:36,640 --> 00:11:40,960 small computational part of it that actually just prepares the thing to 235 00:11:40,960 --> 00:11:42,620 send back to the user. 236 00:11:42,620 --> 00:11:46,710 So does that kind of make sense? 237 00:11:46,710 --> 00:11:49,940 >> But what are really the implications of this? 238 00:11:49,940 --> 00:11:50,660 Right? 239 00:11:50,660 --> 00:11:53,730 Because we've written a lot of code that-- 240 00:11:53,730 --> 00:11:58,330 and I'm just going to jump ahead in the slides if that's OK. 241 00:11:58,330 --> 00:12:00,930 So if you don't mind, I'm just going to take a step back. 242 00:12:00,930 --> 00:12:03,410 So this kind of thing is called an event loop. 243 00:12:03,410 --> 00:12:04,070 OK? 244 00:12:04,070 --> 00:12:07,240 And it's kind of the basic idea behind Node.js. 245 00:12:07,240 --> 00:12:11,240 >> So what Node.js is really doing as a web server is there's a single thread 246 00:12:11,240 --> 00:12:14,850 that is basically going around in a loop like a while one kind of under 247 00:12:14,850 --> 00:12:18,510 the hood of Node.js that's constantly checking, did we receive new things? 248 00:12:18,510 --> 00:12:22,720 And then it will run handlers that you set up. 249 00:12:22,720 --> 00:12:26,720 But a good question to ask is, how can we make this happen 250 00:12:26,720 --> 00:12:28,090 with existing things? 251 00:12:28,090 --> 00:12:32,440 >> So I put a line of C code here that basically looks like it's opening a 252 00:12:32,440 --> 00:12:33,060 file, right? 253 00:12:33,060 --> 00:12:36,090 I She just came out with an album. 254 00:12:36,090 --> 00:12:39,600 So I had to open her a new file. 255 00:12:39,600 --> 00:12:43,810 So the way our C code for operating-- 256 00:12:43,810 --> 00:12:47,890 and I guess the reason I chose files was because this is kind of the extent 257 00:12:47,890 --> 00:12:52,000 of the I/O work that we've done in C in a sense that there's input output. 258 00:12:52,000 --> 00:12:55,070 So we call this code that does this f open. 259 00:12:55,070 --> 00:12:59,370 And then on the next line of our program, we can now work with f. 260 00:12:59,370 --> 00:13:02,710 >> So this would be an example of something that's like synchronous or 261 00:13:02,710 --> 00:13:06,850 blocking because on that first line there we're waiting until we 262 00:13:06,850 --> 00:13:08,110 get the file open. 263 00:13:08,110 --> 00:13:12,260 So on the second line, we know that we can work with f, but this means that 264 00:13:12,260 --> 00:13:16,240 that second line can't really run until the first line is done. 265 00:13:16,240 --> 00:13:17,760 Does that make sense? 266 00:13:17,760 --> 00:13:20,890 >> So this would be bad to put in an event handler. 267 00:13:20,890 --> 00:13:23,920 And the reason for that is that this kind of waits, right? 268 00:13:23,920 --> 00:13:26,500 So this would revert us back to the same thing. 269 00:13:26,500 --> 00:13:29,470 And now we wouldn't even have the benefit of multiple threads or 270 00:13:29,470 --> 00:13:32,390 processes because we got one thread in Node.js. 271 00:13:32,390 --> 00:13:35,496 Does that make sense to everybody? 272 00:13:35,496 --> 00:13:35,990 >> AUDIENCE: Wait. 273 00:13:35,990 --> 00:13:36,980 So what's the replacement? 274 00:13:36,980 --> 00:13:37,840 >> KEVIN SCHMID: Oh, so yes. 275 00:13:37,840 --> 00:13:39,560 So I'm going to get to the replacement. 276 00:13:39,560 --> 00:13:40,430 OK. 277 00:13:40,430 --> 00:13:42,960 So what if we had something that looked like this? 278 00:13:42,960 --> 00:13:45,730 So what if now I edited f open a little? 279 00:13:45,730 --> 00:13:48,370 So I'm passing in the same two arguments as before. 280 00:13:48,370 --> 00:13:52,610 I still love the new song that she came out with. 281 00:13:52,610 --> 00:13:57,260 But I'm passing a third thing which is this variable called code. 282 00:13:57,260 --> 00:14:02,280 >> But what is code actually in this context? 283 00:14:02,280 --> 00:14:05,360 Is it like a regular C variable? 284 00:14:05,360 --> 00:14:06,740 It's a function, right? 285 00:14:06,740 --> 00:14:09,450 And that may be a little weird because I'm actually like now passing a 286 00:14:09,450 --> 00:14:12,320 function into another function. 287 00:14:12,320 --> 00:14:14,400 >> So a couple things to note about this. 288 00:14:14,400 --> 00:14:17,145 One, I'm not actually calling the code function. 289 00:14:17,145 --> 00:14:20,650 So you don't see code with the left paren, right paren. 290 00:14:20,650 --> 00:14:23,010 I'm just passing in code. 291 00:14:23,010 --> 00:14:26,990 And in C, what this would actually do is give me a pointer to that actual 292 00:14:26,990 --> 00:14:29,740 code, and then this could run it. 293 00:14:29,740 --> 00:14:33,350 But just think about it as you're passing the code to run when 294 00:14:33,350 --> 00:14:35,150 that file is opened. 295 00:14:35,150 --> 00:14:41,430 >> But what this means is that now the rest of my program which could do 296 00:14:41,430 --> 00:14:47,050 other stuff, can continue doing other stuff while we, not really wait, but 297 00:14:47,050 --> 00:14:50,890 just have in the back of our heads that when that file's open, run that 298 00:14:50,890 --> 00:14:52,130 code at the top. 299 00:14:52,130 --> 00:14:53,390 Does that make sense? 300 00:14:53,390 --> 00:14:58,060 >> And now the idea behind Node.js is that the code in the do stuff with f 301 00:14:58,060 --> 00:15:04,590 part should be pretty short and simple and straightforward and not really be 302 00:15:04,590 --> 00:15:06,160 very computationally intensive. 303 00:15:06,160 --> 00:15:09,390 It may have to open another file, but that should also be pretty quick 304 00:15:09,390 --> 00:15:14,710 because it should just say do another f open and then call this other code. 305 00:15:14,710 --> 00:15:19,100 >> So just to be completely clear, the f open that does the new Katy Perry song 306 00:15:19,100 --> 00:15:23,060 done mp3, that's going to pretty much return immediately. 307 00:15:23,060 --> 00:15:27,820 And then we can just continue doing other stuff because all that now f 308 00:15:27,820 --> 00:15:33,410 open call does is tell basically the underlying f open code open this file 309 00:15:33,410 --> 00:15:36,020 and when you're done opening this file or when you get it back, 310 00:15:36,020 --> 00:15:37,480 then run that code. 311 00:15:37,480 --> 00:15:39,540 But it doesn't actually run that code. 312 00:15:39,540 --> 00:15:41,815 And you had a question? 313 00:15:41,815 --> 00:15:46,180 >> AUDIENCE: You seemed to imply a few times that adding computationally 314 00:15:46,180 --> 00:15:50,545 intensive code sort of break the [INAUDIBLE] driven system. 315 00:15:50,545 --> 00:15:51,795 [INAUDIBLE]? 316 00:15:51,795 --> 00:15:54,450 317 00:15:54,450 --> 00:15:55,290 >> KEVIN SCHMID: That's a great question. 318 00:15:55,290 --> 00:15:59,280 So I actually have an example of how you could integrate computationally 319 00:15:59,280 --> 00:16:01,090 intensive code in a little bit. 320 00:16:01,090 --> 00:16:03,620 So when we get to the code examples, I'll be sure to pull that one. 321 00:16:03,620 --> 00:16:04,700 Is that OK? 322 00:16:04,700 --> 00:16:05,950 Thank you. 323 00:16:05,950 --> 00:16:07,690 324 00:16:07,690 --> 00:16:08,750 >> What was your name? 325 00:16:08,750 --> 00:16:10,620 >> AUDIENCE: Aaron. 326 00:16:10,620 --> 00:16:14,830 >> KEVIN SCHMID: Aaron brings up a very good point, which is that if I had 327 00:16:14,830 --> 00:16:18,560 some computationally intensive code in the do stuff with f part, the rest of 328 00:16:18,560 --> 00:16:22,880 my program can't run and can't listen for new requests or anything until all 329 00:16:22,880 --> 00:16:24,270 that stuff is finished. 330 00:16:24,270 --> 00:16:27,390 So if I'm writing Node code in general unless we do something like I'm going 331 00:16:27,390 --> 00:16:33,060 to suggest later when we look at the code examples, I have to be sure that 332 00:16:33,060 --> 00:16:36,060 my code doesn't tie up this event loop. 333 00:16:36,060 --> 00:16:38,120 Does that make sense? 334 00:16:38,120 --> 00:16:38,350 OK. 335 00:16:38,350 --> 00:16:40,040 Cool. 336 00:16:40,040 --> 00:16:47,090 >> So Node.js offers this framework that you can build these event driven 337 00:16:47,090 --> 00:16:48,210 servers with. 338 00:16:48,210 --> 00:16:53,460 So it has these kind of asynchronous non-blocking I/O libraries, whereas 339 00:16:53,460 --> 00:16:56,800 the standard C libraries that we've been working with, like if you just 340 00:16:56,800 --> 00:16:59,500 use them in the same way that we've been using them with f opens and 341 00:16:59,500 --> 00:17:03,000 stuff, those are blocking because you actually have to wait for 342 00:17:03,000 --> 00:17:04,470 that file to open. 343 00:17:04,470 --> 00:17:09,290 >> But Node.js gives you that and it basically ties into Google's V8 344 00:17:09,290 --> 00:17:14,030 JavaScript engine which is the reason that Chrome is so fast at processing 345 00:17:14,030 --> 00:17:17,040 JavaScript because it has this V8 engine. 346 00:17:17,040 --> 00:17:22,460 So I know that sounds like one of those WWDC developer conferences thing 347 00:17:22,460 --> 00:17:25,390 where they just throw a bunch of the letter number things for processors 348 00:17:25,390 --> 00:17:26,910 and say this is so cool. 349 00:17:26,910 --> 00:17:34,200 But it is cool that they did this because JavaScript-- 350 00:17:34,200 --> 00:17:37,010 or maybe if you're not familiar with JavaScript yet because we haven't had 351 00:17:37,010 --> 00:17:38,180 the lectures on it-- 352 00:17:38,180 --> 00:17:40,770 but JavaScript is an interpreted language. 353 00:17:40,770 --> 00:17:41,970 >> And this is an important point too. 354 00:17:41,970 --> 00:17:45,790 So it's important for our web servers to be fast, right? 355 00:17:45,790 --> 00:17:49,970 And if we were just running JavaScript code that was interpreted with just 356 00:17:49,970 --> 00:17:52,130 any old interpreter it might be slow. 357 00:17:52,130 --> 00:17:55,980 So Node benefits from having this super fast V8 interpreter. 358 00:17:55,980 --> 00:17:59,580 And I don't know if they named it because the V8 slap in the forehead 359 00:17:59,580 --> 00:18:01,110 thing, but OK. 360 00:18:01,110 --> 00:18:07,070 >> So I've prepared some examples at this URL. 361 00:18:07,070 --> 00:18:10,490 After the seminar, I'm sort of going to talk about how you can get Node set 362 00:18:10,490 --> 00:18:13,570 up, but for now, I just sort of want to walk through some code examples. 363 00:18:13,570 --> 00:18:17,250 So if you want to follow along, all the source code is available there. 364 00:18:17,250 --> 00:18:18,720 OK? 365 00:18:18,720 --> 00:18:22,280 >> So I'll leave this URL up for a little. 366 00:18:22,280 --> 00:18:24,440 And then I'm just going to switch into the terminal. 367 00:18:24,440 --> 00:18:29,670 368 00:18:29,670 --> 00:18:34,400 Is everybody good with this URL? 369 00:18:34,400 --> 00:18:37,990 So I'm going to switch over to my terminal here. 370 00:18:37,990 --> 00:18:42,030 >> So here's the code that I have for today. 371 00:18:42,030 --> 00:18:43,960 Why don't we start with simpler.js file? 372 00:18:43,960 --> 00:18:49,110 373 00:18:49,110 --> 00:18:52,100 The other thing is that all of this code is going to be written in 374 00:18:52,100 --> 00:18:56,660 JavaScript which you may or may not be familiar with. 375 00:18:56,660 --> 00:19:00,170 I guess a couple things is that a lot of JavaScript code is the kind of 376 00:19:00,170 --> 00:19:04,000 syntax and structure is very similar to C, so you can kind of pick it up as 377 00:19:04,000 --> 00:19:05,020 you go along. 378 00:19:05,020 --> 00:19:08,750 I've tried to write a lot of the starting code for this in a way that's 379 00:19:08,750 --> 00:19:11,230 similar to C so that it's a little more readable. 380 00:19:11,230 --> 00:19:15,980 But as we progress, I'll be demonstrating some of the additional 381 00:19:15,980 --> 00:19:18,980 features of JavaScript that are kind of cool. 382 00:19:18,980 --> 00:19:21,510 >> But let's look at this sample program. 383 00:19:21,510 --> 00:19:24,820 I guess everything's cut off there. 384 00:19:24,820 --> 00:19:28,500 I'm just going to fix that real fast if that's OK or not. 385 00:19:28,500 --> 00:19:31,400 I don't know what this is going to do. 386 00:19:31,400 --> 00:19:34,660 Is that a little better? 387 00:19:34,660 --> 00:19:36,510 Can you see the var and stuff? 388 00:19:36,510 --> 00:19:39,320 OK. 389 00:19:39,320 --> 00:19:44,120 >> So the first line is like the JavaScript version of a variable 390 00:19:44,120 --> 00:19:44,800 declaration. 391 00:19:44,800 --> 00:19:49,870 So just to highlight what this would look like in C. So this is just like 392 00:19:49,870 --> 00:19:52,620 me saying index equals three or something like that. 393 00:19:52,620 --> 00:19:55,740 So I didn't specify the type. 394 00:19:55,740 --> 00:20:00,780 JavaScript does have types, but it's very dynamically typed in nature, so 395 00:20:00,780 --> 00:20:02,580 didn't provide any kind of type on it. 396 00:20:02,580 --> 00:20:03,670 So it just has var. 397 00:20:03,670 --> 00:20:05,320 That's like variable. 398 00:20:05,320 --> 00:20:05,920 OK? 399 00:20:05,920 --> 00:20:08,340 >> And I'm calling this variable HTTP. 400 00:20:08,340 --> 00:20:12,480 And on my right hand side, I have the expression that I want to put in HTTP. 401 00:20:12,480 --> 00:20:14,960 And this says require HTTP. 402 00:20:14,960 --> 00:20:18,500 So this is kind of similar to include. 403 00:20:18,500 --> 00:20:22,940 It's a little more like powerful than include in the sense that include 404 00:20:22,940 --> 00:20:26,100 would just copy and paste the header file for the function prototypes or 405 00:20:26,100 --> 00:20:27,930 whatever with the type definitions. 406 00:20:27,930 --> 00:20:30,590 But require is actually going to get us the code. 407 00:20:30,590 --> 00:20:33,280 >> So you can think of it as importing some code. 408 00:20:33,280 --> 00:20:37,960 So somewhere in the Node.js module system or whatever, they have all this 409 00:20:37,960 --> 00:20:40,790 HTTP server code so I'm just fetching it for my own 410 00:20:40,790 --> 00:20:43,130 personal use in this program. 411 00:20:43,130 --> 00:20:44,260 OK? 412 00:20:44,260 --> 00:20:46,930 >> So then I have this function that I've written. 413 00:20:46,930 --> 00:20:50,330 And notice I didn't have to specify the return type or the type of the 414 00:20:50,330 --> 00:20:51,140 arguments again. 415 00:20:51,140 --> 00:20:54,440 So kind of loose typed in that kind of sense. 416 00:20:54,440 --> 00:20:57,290 Two arguments that it takes in, the request and response. 417 00:20:57,290 --> 00:21:02,080 So that's conceptually kind of like familiar from the picture that we had 418 00:21:02,080 --> 00:21:05,280 on the screen before because we get this request that we 419 00:21:05,280 --> 00:21:06,410 have from the user. 420 00:21:06,410 --> 00:21:09,170 And then we have a response that we can write things to. 421 00:21:09,170 --> 00:21:15,060 >> So the first line of this does res.writeHead 200 and then this 422 00:21:15,060 --> 00:21:17,070 content type text plain. 423 00:21:17,070 --> 00:21:19,300 So let's piece this apart a little. 424 00:21:19,300 --> 00:21:22,340 So let's just focus on res.write for a little. 425 00:21:22,340 --> 00:21:28,420 So write is basically, and write head, are just ways to sort of write out 426 00:21:28,420 --> 00:21:29,960 things to the response. 427 00:21:29,960 --> 00:21:30,770 OK? 428 00:21:30,770 --> 00:21:36,230 So write head, if anybody remembers from the HTTP lecture, do you guys 429 00:21:36,230 --> 00:21:39,940 remember headers at the top of the HTTP thing? 430 00:21:39,940 --> 00:21:43,580 So why don't I just demo headers real quick. 431 00:21:43,580 --> 00:21:44,640 Would that be helpful? 432 00:21:44,640 --> 00:21:45,500 Or should we just sort of-- 433 00:21:45,500 --> 00:21:46,070 OK. 434 00:21:46,070 --> 00:21:46,740 Sure. 435 00:21:46,740 --> 00:21:52,340 >> So when your browser goes to google.com or something like that, 436 00:21:52,340 --> 00:21:54,250 there's actually a little more-- 437 00:21:54,250 --> 00:21:55,380 this is like a secret-- 438 00:21:55,380 --> 00:21:58,340 there's like a little more information that comes through the pipe than just 439 00:21:58,340 --> 00:22:00,180 the little search and everything. 440 00:22:00,180 --> 00:22:03,550 So to show you this, I'm going to use a program called Curl. 441 00:22:03,550 --> 00:22:04,260 OK? 442 00:22:04,260 --> 00:22:08,020 So this is something that you can run at your Mac OSX command line or in the 443 00:22:08,020 --> 00:22:09,830 appliance or whatever. 444 00:22:09,830 --> 00:22:17,050 And so if I do Curl HTTP google.com, I'm going to see the HTML. 445 00:22:17,050 --> 00:22:21,230 And this is, in fairness, just the HTML that sort of tells you to 446 00:22:21,230 --> 00:22:24,695 redirect to www if your browser doesn't automatically handle the 447 00:22:24,695 --> 00:22:27,110 redirection. 448 00:22:27,110 --> 00:22:33,390 >> So this is just HTML, but I'm going to add to Curl this hyphen I flag. 449 00:22:33,390 --> 00:22:33,600 OK? 450 00:22:33,600 --> 00:22:35,600 And this is going to show me the headers. 451 00:22:35,600 --> 00:22:40,640 So this is also information that comes through when I get this response. 452 00:22:40,640 --> 00:22:41,260 OK? 453 00:22:41,260 --> 00:22:45,320 >> So at the top, you see this HTTP 301 move permanently. 454 00:22:45,320 --> 00:22:49,470 And this is kind of important because this refers to the status code. 455 00:22:49,470 --> 00:22:53,750 So the 301 here is the status code, which is basically just an integer 456 00:22:53,750 --> 00:22:57,750 that tells the browser or whoever's reading this, if you pretend that 457 00:22:57,750 --> 00:23:01,460 you're a browser and you're seeing this, basically now if you look at 458 00:23:01,460 --> 00:23:04,960 that and you see a 301, you know I have to do something special based on 459 00:23:04,960 --> 00:23:08,810 301, or something special happened based on the 301. 460 00:23:08,810 --> 00:23:12,640 So it says moved permanently. 461 00:23:12,640 --> 00:23:17,700 >> And then, basically, we have a bunch of key value pairs. 462 00:23:17,700 --> 00:23:22,100 So we get the location is www.google.com. 463 00:23:22,100 --> 00:23:25,190 And then kind of all this other stuff, but basically, what the location is 464 00:23:25,190 --> 00:23:29,662 saying is the new location is at www.google.com. 465 00:23:29,662 --> 00:23:33,800 So now if you go to google.com, you'll sort of see the browser kind of blink 466 00:23:33,800 --> 00:23:38,770 for a second and then redirect you right back to www.google.com. 467 00:23:38,770 --> 00:23:41,840 So the responses can contain these headers. 468 00:23:41,840 --> 00:23:43,330 >> And a couple of things to point out. 469 00:23:43,330 --> 00:23:46,890 So let's say we were actually successful in visiting a web page. 470 00:23:46,890 --> 00:23:49,040 So let me go to-- 471 00:23:49,040 --> 00:23:51,080 what's a good website? 472 00:23:51,080 --> 00:23:53,285 I'm bad at thinking of good websites on the spot. 473 00:23:53,285 --> 00:23:53,640 >> AUDIENCE: Wikipedia. 474 00:23:53,640 --> 00:23:54,160 >> KEVIN SCHMID: OK. 475 00:23:54,160 --> 00:23:56,040 Let's do Wikipedia. 476 00:23:56,040 --> 00:23:58,680 So here I was moved. 477 00:23:58,680 --> 00:23:59,240 Oh wait. 478 00:23:59,240 --> 00:24:00,160 Was I? 479 00:24:00,160 --> 00:24:00,890 Yes, I was. 480 00:24:00,890 --> 00:24:01,100 OK. 481 00:24:01,100 --> 00:24:03,005 So I got to do www. 482 00:24:03,005 --> 00:24:06,006 So I'm going to do www. 483 00:24:06,006 --> 00:24:09,680 And as you can see, here's all the HTML that the browser would process 484 00:24:09,680 --> 00:24:10,910 for Wikipedia. 485 00:24:10,910 --> 00:24:14,055 >> But if I keep scrolling up here, what I'll see at the top-- 486 00:24:14,055 --> 00:24:17,800 wow, there's a lot of HTML on Wikipedia-- 487 00:24:17,800 --> 00:24:22,550 but what I can see at the top here is this 200 status code as opposed to the 488 00:24:22,550 --> 00:24:24,570 301 that I saw earlier. 489 00:24:24,570 --> 00:24:27,100 And notice that it has a nice friendly OK next to it. 490 00:24:27,100 --> 00:24:29,470 So this is like the good status code. 491 00:24:29,470 --> 00:24:31,160 >> Does that 200 number look familiar? 492 00:24:31,160 --> 00:24:34,120 493 00:24:34,120 --> 00:24:39,880 Yes because when I did simpler.js, I wrote a 200 there. 494 00:24:39,880 --> 00:24:43,290 So that's basically saying tell the browser or whoever is trying to get to 495 00:24:43,290 --> 00:24:45,440 this that they were successful. 496 00:24:45,440 --> 00:24:49,040 Or that kind of like we were successful too. 497 00:24:49,040 --> 00:24:54,320 >> And there's this kind of special syntax in Javascript for declaring a 498 00:24:54,320 --> 00:24:59,870 map of these keys like content type and these values like text plain. 499 00:24:59,870 --> 00:25:03,780 So if you look at the response that we got back from Wikipedia before,-- 500 00:25:03,780 --> 00:25:06,200 I'm going to try to scroll up a little faster-- 501 00:25:06,200 --> 00:25:09,900 you have these keys like server and these values Apache. 502 00:25:09,900 --> 00:25:12,120 So you've got keys and values. 503 00:25:12,120 --> 00:25:15,930 And you can specify this in Node what to send back. 504 00:25:15,930 --> 00:25:19,380 >> So this is actually kind of, in some ways, and in some ways it's not 505 00:25:19,380 --> 00:25:23,170 really, but it's a little lower level than the PHP code that you might be 506 00:25:23,170 --> 00:25:26,980 writing for P set seven because PHP and Apache sort of take care of some 507 00:25:26,980 --> 00:25:28,150 of these things for you. 508 00:25:28,150 --> 00:25:32,520 In PHP, you can override the default behavior by writing your own headers. 509 00:25:32,520 --> 00:25:35,520 But for the purposes of this, we get to write out our own headers. 510 00:25:35,520 --> 00:25:38,210 511 00:25:38,210 --> 00:25:41,105 >> So does that line make sense to everybody, the write head line? 512 00:25:41,105 --> 00:25:41,380 OK. 513 00:25:41,380 --> 00:25:42,280 Awesome. 514 00:25:42,280 --> 00:25:45,870 >> So then what I do is I end the response by saying hello world. 515 00:25:45,870 --> 00:25:47,040 OK. 516 00:25:47,040 --> 00:25:49,920 But that's just a function called request handler. 517 00:25:49,920 --> 00:25:53,510 So now I actually have to kind of do something with this function, right? 518 00:25:53,510 --> 00:25:59,170 >> So here what I do is there is this line which does var server equals 519 00:25:59,170 --> 00:26:03,530 HTTP.create server, and then I pass in the request handler. 520 00:26:03,530 --> 00:26:06,080 So this is kind of the Node way of creating a server. 521 00:26:06,080 --> 00:26:08,790 And notice that I'm passing in the request handler. 522 00:26:08,790 --> 00:26:12,290 So this is telling the createServer function that I want you to make me a 523 00:26:12,290 --> 00:26:16,270 server, and when that server receives a response, I need you to call this 524 00:26:16,270 --> 00:26:18,680 request handler function. 525 00:26:18,680 --> 00:26:18,990 OK? 526 00:26:18,990 --> 00:26:22,290 >> So that line pretty much finishes right away. 527 00:26:22,290 --> 00:26:28,780 So the var server line is done right after you do that pretty much. 528 00:26:28,780 --> 00:26:31,770 I mean, it has to set up some internal state to know that you would have to 529 00:26:31,770 --> 00:26:35,400 call that request handler function, but it's not going to sit there and 530 00:26:35,400 --> 00:26:37,730 say has the user sent me a request yet? 531 00:26:37,730 --> 00:26:39,270 Has the user sent me a request yet? 532 00:26:39,270 --> 00:26:40,780 So it doesn't block. 533 00:26:40,780 --> 00:26:41,650 OK? 534 00:26:41,650 --> 00:26:46,120 >> So what this will do is it basically now stores a pointer to this code, 535 00:26:46,120 --> 00:26:49,670 this request handler function, and then will run that code when somebody 536 00:26:49,670 --> 00:26:52,170 makes a request. 537 00:26:52,170 --> 00:26:54,120 And then we do server.listen. 538 00:26:54,120 --> 00:26:56,950 539 00:26:56,950 --> 00:26:59,960 >> The 1337 there is pretty arbitrary. 540 00:26:59,960 --> 00:27:02,285 I had no particular reason for picking that number. 541 00:27:02,285 --> 00:27:03,860 It was totally random. 542 00:27:03,860 --> 00:27:07,010 But that just specifies the port. 543 00:27:07,010 --> 00:27:10,640 So most web servers you'll see that they use port 80 because that's kind 544 00:27:10,640 --> 00:27:11,810 of like the convention. 545 00:27:11,810 --> 00:27:16,170 So if I go to something like, I don't know, Wikipedia.org, 546 00:27:16,170 --> 00:27:17,700 and I put colon 8-- 547 00:27:17,700 --> 00:27:18,610 oh wow, you can't see that. 548 00:27:18,610 --> 00:27:19,370 I'm sorry. 549 00:27:19,370 --> 00:27:21,820 But if I do Wikipedia-- 550 00:27:21,820 --> 00:27:24,810 I'll write it here just so that it's clear on the camera. 551 00:27:24,810 --> 00:27:29,150 But if I take this into a browser with a colon 80, that specifies go to 552 00:27:29,150 --> 00:27:31,430 Wikipedia.org at port 80. 553 00:27:31,430 --> 00:27:36,200 So it's like how the United States has multiple ports like where you can ship 554 00:27:36,200 --> 00:27:37,440 things to kind of. 555 00:27:37,440 --> 00:27:40,730 So it's like go to this particular place on this server. 556 00:27:40,730 --> 00:27:40,990 OK. 557 00:27:40,990 --> 00:27:45,730 >> So I just chose 1337. 558 00:27:45,730 --> 00:27:47,910 There's a whole range of numbers that you can pick. 559 00:27:47,910 --> 00:27:50,390 That wasn't totally special. 560 00:27:50,390 --> 00:27:54,560 >> But what I'm going to do now is I'm going to run Node. 561 00:27:54,560 --> 00:27:59,730 Let me actually enter that a couple lines down so that you can see it. 562 00:27:59,730 --> 00:28:03,130 I'm going to do Node, and I'm going to run simpler.js. 563 00:28:03,130 --> 00:28:06,880 And we'll talk about how to get Node set up in a little bit. 564 00:28:06,880 --> 00:28:09,350 But now it's just running the server. 565 00:28:09,350 --> 00:28:14,360 >> So one thing we can try which may not be that exciting is we can actually 566 00:28:14,360 --> 00:28:16,300 try to access it in Curl. 567 00:28:16,300 --> 00:28:20,680 So I can do Curl, and my machine is local host. 568 00:28:20,680 --> 00:28:24,600 You'll also see this written like this sometimes. 569 00:28:24,600 --> 00:28:29,810 Local host and 127.0.0.1 are kind of like your home computer. 570 00:28:29,810 --> 00:28:33,180 So it's like talking to your own computer. 571 00:28:33,180 --> 00:28:33,760 OK. 572 00:28:33,760 --> 00:28:36,030 >> And then I can say 1337. 573 00:28:36,030 --> 00:28:39,630 So if I run this line of code, it says hello world. 574 00:28:39,630 --> 00:28:44,050 And if I wanted to see that stuff that had content type text plain or 575 00:28:44,050 --> 00:28:46,560 whatever, I could even put this here. 576 00:28:46,560 --> 00:28:48,810 And notice that it does say OK. 577 00:28:48,810 --> 00:28:50,810 And I do have text plain. 578 00:28:50,810 --> 00:28:53,140 And then there's kind of all this other stuff that Node will add in 579 00:28:53,140 --> 00:28:54,440 there for me. 580 00:28:54,440 --> 00:28:55,700 That's not super important. 581 00:28:55,700 --> 00:28:58,230 >> I mean, there are some kind of technical aspects of at that are kind 582 00:28:58,230 --> 00:29:02,280 of cool to talk about, but just to show you, I also have the power to 583 00:29:02,280 --> 00:29:03,070 change these around. 584 00:29:03,070 --> 00:29:06,280 So I can just add a bunch of stuff like that. 585 00:29:06,280 --> 00:29:11,780 And then now, if I look in my output, it will be that. 586 00:29:11,780 --> 00:29:19,740 So these headers mean certain things to browsers and things like that. 587 00:29:19,740 --> 00:29:23,040 >> And headers can basically tell a browser how to respond to something. 588 00:29:23,040 --> 00:29:26,280 If you've ever heard of cookies before, or if you've ever been annoyed 589 00:29:26,280 --> 00:29:29,330 by a web page setting cookies, or turned on cookie block or 590 00:29:29,330 --> 00:29:30,320 something like that. 591 00:29:30,320 --> 00:29:33,040 You can actually set cookies in these headers. 592 00:29:33,040 --> 00:29:36,990 So they tell a browser how to behavior in some cases. 593 00:29:36,990 --> 00:29:37,750 OK. 594 00:29:37,750 --> 00:29:40,310 >> So that was simpler.js. 595 00:29:40,310 --> 00:29:42,780 Does anybody have any questions on that source code file? 596 00:29:42,780 --> 00:29:45,420 597 00:29:45,420 --> 00:29:45,610 OK. 598 00:29:45,610 --> 00:29:46,490 Cool. 599 00:29:46,490 --> 00:29:50,780 >> So let's remove the r from that and look at simple.js. 600 00:29:50,780 --> 00:29:53,010 So this is pretty much the same program. 601 00:29:53,010 --> 00:29:56,030 I just wrote it a little differently because I wanted to sort of highlight 602 00:29:56,030 --> 00:29:57,850 some features of JavaScript. 603 00:29:57,850 --> 00:30:03,880 >> So notice that the request handler function has totally vanished. 604 00:30:03,880 --> 00:30:05,800 Oh yep, did you have a question? 605 00:30:05,800 --> 00:30:08,200 >> AUDIENCE: Yeah, the arguments that are passed to that 606 00:30:08,200 --> 00:30:10,120 function, what are they? 607 00:30:10,120 --> 00:30:12,050 >> KEVIN SCHMID: So those are JavaScript objects. 608 00:30:12,050 --> 00:30:15,230 In the Node.js documentation, it basically says what methods are 609 00:30:15,230 --> 00:30:15,910 available on them. 610 00:30:15,910 --> 00:30:19,602 We just happen to have the access to this method called write head and end 611 00:30:19,602 --> 00:30:20,730 and stuff like that. 612 00:30:20,730 --> 00:30:22,590 But there's a whole bunch more methods. 613 00:30:22,590 --> 00:30:27,670 >> And for example, like one of them in particular on rec, you can do 614 00:30:27,670 --> 00:30:34,540 something like rec.method which will tell you whether it's a HTTP get or 615 00:30:34,540 --> 00:30:36,780 HTTP post requests and things like that. 616 00:30:36,780 --> 00:30:39,100 So there's all kinds of different properties, but they're both 617 00:30:39,100 --> 00:30:42,560 JavaScript objects, and they just have functions attached to them that you 618 00:30:42,560 --> 00:30:43,850 can write things to. 619 00:30:43,850 --> 00:30:45,520 OK? 620 00:30:45,520 --> 00:30:49,030 >> So notice that request handler is totally gone. 621 00:30:49,030 --> 00:30:52,650 But the code that I had in request handler is still there. 622 00:30:52,650 --> 00:30:56,520 I still have this res.writeHead and I still have this res.end. 623 00:30:56,520 --> 00:31:00,270 And what this is an example of in JavaScript is this idea of an 624 00:31:00,270 --> 00:31:01,460 anonymous function. 625 00:31:01,460 --> 00:31:04,180 and anonymous is like a fitting name for it because it literally doesn't 626 00:31:04,180 --> 00:31:05,180 have a name. 627 00:31:05,180 --> 00:31:07,900 There's no function request handler in there. 628 00:31:07,900 --> 00:31:10,110 >> Has no name, but it still is taking an argument. 629 00:31:10,110 --> 00:31:12,250 So I still got rec and res. 630 00:31:12,250 --> 00:31:16,180 And I still have the code. 631 00:31:16,180 --> 00:31:18,930 This is perfectly fine JavaScript code. 632 00:31:18,930 --> 00:31:22,540 So I can declare a function without explicitly giving it a name. 633 00:31:22,540 --> 00:31:24,250 It's a little confusing at first. 634 00:31:24,250 --> 00:31:26,230 There are some like useful things that you can do with 635 00:31:26,230 --> 00:31:28,450 these anonymous functions. 636 00:31:28,450 --> 00:31:32,100 Does anybody have any questions on this, or is it OK just to, for now, 637 00:31:32,100 --> 00:31:34,130 sort of just accept that it will do the same thing? 638 00:31:34,130 --> 00:31:36,700 639 00:31:36,700 --> 00:31:37,125 Yep? 640 00:31:37,125 --> 00:31:38,680 >> AUDIENCE: Are functions first class in JavaScript? 641 00:31:38,680 --> 00:31:41,020 >> KEVIN SCHMID: They are first class in JavaScript. 642 00:31:41,020 --> 00:31:45,490 And just know that these concepts of passing in an anonymous function like 643 00:31:45,490 --> 00:31:49,600 this apply to the JavaScript that you may write in your final project for 644 00:31:49,600 --> 00:31:51,260 the web browser too. 645 00:31:51,260 --> 00:31:56,700 So for example, in the JavaScript in your browser, it's also somewhat event 646 00:31:56,700 --> 00:32:00,680 driven in the sense that what you'll have is when the user clicks this 647 00:32:00,680 --> 00:32:02,640 button, I want you to run this code. 648 00:32:02,640 --> 00:32:07,070 >> So it's the same kind of ideas of the client side when a mouse click or they 649 00:32:07,070 --> 00:32:09,870 mouse over some image on your web page, run this code. 650 00:32:09,870 --> 00:32:11,350 That can apply to servers. 651 00:32:11,350 --> 00:32:16,380 So that's kind of like the exciting reason why JavaScript is a really 652 00:32:16,380 --> 00:32:19,810 suitable or some people think it's a suitable language for this kind of 653 00:32:19,810 --> 00:32:22,530 event driver server because you have these anonymous functions. 654 00:32:22,530 --> 00:32:26,150 You have the whole idea of this asynchronous code. 655 00:32:26,150 --> 00:32:27,060 OK. 656 00:32:27,060 --> 00:32:30,360 Anybody have any questions? 657 00:32:30,360 --> 00:32:30,470 >> OK. 658 00:32:30,470 --> 00:32:33,440 So that was simple.js. 659 00:32:33,440 --> 00:32:38,070 So let's look at one more or a couple more. 660 00:32:38,070 --> 00:32:42,040 So this is sleep.js. 661 00:32:42,040 --> 00:32:47,160 So is anybody familiar with the C function sleep? 662 00:32:47,160 --> 00:32:50,936 From maybe one of the earlier lectures or something like that? 663 00:32:50,936 --> 00:32:54,650 >> So basically you can pass in I think a number of seconds or if you're using U 664 00:32:54,650 --> 00:32:57,080 sleep a number of milliseconds or nanoseconds. 665 00:32:57,080 --> 00:33:00,450 And basically the program will just stop running for that amount of time. 666 00:33:00,450 --> 00:33:01,280 Right? 667 00:33:01,280 --> 00:33:06,970 And then it will wake up eventually and then it'll just continue running 668 00:33:06,970 --> 00:33:08,340 the program. 669 00:33:08,340 --> 00:33:12,740 >> So this server sort of gives the impression of sleeping. 670 00:33:12,740 --> 00:33:17,580 So notice that we have the same res.writeHead 200 with the header as 671 00:33:17,580 --> 00:33:22,130 before, but then we're calling this function called set timeout. 672 00:33:22,130 --> 00:33:26,170 Set timeout is also available in your web browser Google Chrome 673 00:33:26,170 --> 00:33:28,000 or Safari or whatever. 674 00:33:28,000 --> 00:33:31,720 And basically what it's doing here is it's taking in a function. 675 00:33:31,720 --> 00:33:33,360 Notice, again, it's an anonymous function. 676 00:33:33,360 --> 00:33:36,310 So that's kind of cool because we're using an anonymous function within an 677 00:33:36,310 --> 00:33:38,950 anonymous function which can be a little weird. 678 00:33:38,950 --> 00:33:42,270 >> But it's taking that function, which is basically saying-- and the way this 679 00:33:42,270 --> 00:33:47,430 works is in 5,000 milliseconds, I want you to execute that function which 680 00:33:47,430 --> 00:33:50,830 just ends the response and writes hey. 681 00:33:50,830 --> 00:33:56,730 So this gives the impression of like sleeping, but the way this actually 682 00:33:56,730 --> 00:33:59,780 works is we'll run through this line very quickly. 683 00:33:59,780 --> 00:34:01,190 We're just writing something. 684 00:34:01,190 --> 00:34:03,780 And then we'll also run through this line very quickly. 685 00:34:03,780 --> 00:34:08,620 So we're not actually going to wait five seconds. 686 00:34:08,620 --> 00:34:11,370 We're just going to run this code instantly. 687 00:34:11,370 --> 00:34:14,219 >> And then there's, again, this little event loop that now has this thing 688 00:34:14,219 --> 00:34:17,570 registers that basically is just constantly going around in a circle 689 00:34:17,570 --> 00:34:21,620 and looking at the clock in a single thread and saying, has five seconds 690 00:34:21,620 --> 00:34:22,360 passed yet? 691 00:34:22,360 --> 00:34:26,409 And then when it sees that the second hand has moved like five seconds or 692 00:34:26,409 --> 00:34:29,190 whatever, then it wakes up and says, oh, what do I have to do? 693 00:34:29,190 --> 00:34:30,350 Oh I have to run this code. 694 00:34:30,350 --> 00:34:33,110 And then it's going to run res.end hey. 695 00:34:33,110 --> 00:34:35,360 >> So again, we're never waiting here. 696 00:34:35,360 --> 00:34:38,590 So it's not that this code inside of this function is going to take five 697 00:34:38,590 --> 00:34:39,900 seconds to run. 698 00:34:39,900 --> 00:34:43,090 This code will run pretty much instantaneously, at least relative to 699 00:34:43,090 --> 00:34:46,139 the five seconds that we were talking about earlier before. 700 00:34:46,139 --> 00:34:52,100 >> So just to show this in action, I can do Node.sleep.js. 701 00:34:52,100 --> 00:34:55,159 And did I mess up something? 702 00:34:55,159 --> 00:34:56,310 Possibly. 703 00:34:56,310 --> 00:34:57,410 Sorry. 704 00:34:57,410 --> 00:34:59,530 Let's see what we can do to fix this. 705 00:34:59,530 --> 00:35:10,830 706 00:35:10,830 --> 00:35:12,080 OK. 707 00:35:12,080 --> 00:35:15,460 708 00:35:15,460 --> 00:35:17,464 So definitely use Node.js. 709 00:35:17,464 --> 00:35:19,440 I'm just kidding. 710 00:35:19,440 --> 00:35:19,570 OK. 711 00:35:19,570 --> 00:35:20,820 Just one sec. 712 00:35:20,820 --> 00:35:27,380 713 00:35:27,380 --> 00:35:27,900 OK. 714 00:35:27,900 --> 00:35:29,130 I know what it is. 715 00:35:29,130 --> 00:35:34,440 >> So the issue is that in my other tab here, I was running Node already on 716 00:35:34,440 --> 00:35:36,590 that same address, 1337. 717 00:35:36,590 --> 00:35:43,370 So the error that this threw, if we look at it real closely, is address in 718 00:35:43,370 --> 00:35:45,180 use, EADDRINUSE. 719 00:35:45,180 --> 00:35:47,970 So I was already using 1337 here. 720 00:35:47,970 --> 00:35:52,210 So if I shut this off, and then I now try to run this, hopefully, everything 721 00:35:52,210 --> 00:35:53,210 will be fine. 722 00:35:53,210 --> 00:35:53,440 OK. 723 00:35:53,440 --> 00:35:57,020 So you can only have one thing sort of listening on a port at once. 724 00:35:57,020 --> 00:35:59,660 Another solution would have been for me to just edit that program and make 725 00:35:59,660 --> 00:36:02,370 it be like 1338 or something like that. 726 00:36:02,370 --> 00:36:04,100 >> But now sleep is running. 727 00:36:04,100 --> 00:36:06,400 So let's actually try it out in the browser this time because it's a 728 00:36:06,400 --> 00:36:09,080 little unexciting to see it in a terminal. 729 00:36:09,080 --> 00:36:13,560 So I'm just going to go to that 127 address again at 1337. 730 00:36:13,560 --> 00:36:14,850 And if you can see it-- 731 00:36:14,850 --> 00:36:18,050 I don't know if you can-- but my browser's taking a very, very long 732 00:36:18,050 --> 00:36:20,600 time to load or like five seconds. 733 00:36:20,600 --> 00:36:23,460 >> And then after that, it finally ended the response. 734 00:36:23,460 --> 00:36:29,070 And you can't see it because the thing is moved over a little, but if I make 735 00:36:29,070 --> 00:36:32,500 this a little smaller, you can see it says hey. 736 00:36:32,500 --> 00:36:35,130 So I got the hey, but after five seconds. 737 00:36:35,130 --> 00:36:38,510 And it might be a little cleaner to see it here on the terminal, so I'm 738 00:36:38,510 --> 00:36:40,980 going to do a-- 739 00:36:40,980 --> 00:36:43,540 let's do in here-- 740 00:36:43,540 --> 00:36:48,370 let's do Curl that address again with the 1337. 741 00:36:48,370 --> 00:36:50,820 And I just kind of have to sit here for five seconds. 742 00:36:50,820 --> 00:36:53,760 But notice that the server can accept new responses. 743 00:36:53,760 --> 00:36:54,940 So it prints hey. 744 00:36:54,940 --> 00:36:58,720 >> And to demo this, basically what I can do in this other tab-- 745 00:36:58,720 --> 00:37:02,640 so let's say I do this in another tab, I'm going to do Curl and the same 746 00:37:02,640 --> 00:37:03,780 thing again. 747 00:37:03,780 --> 00:37:06,600 And I'm going to try to kick these guys off at the same time. 748 00:37:06,600 --> 00:37:08,260 So I'm going to do this, and I'm going to race over here and I'm 749 00:37:08,260 --> 00:37:09,650 going to do it again. 750 00:37:09,650 --> 00:37:14,030 >> And let's make it so that you can see both of them. 751 00:37:14,030 --> 00:37:20,250 That one printed hey and that one printed hey all the way at-- 752 00:37:20,250 --> 00:37:22,550 let's do that experiment again. 753 00:37:22,550 --> 00:37:25,100 Actually, let's use this trick, if that's OK. 754 00:37:25,100 --> 00:37:30,520 >> So I'm going to use a shell thing that allows me to basically run two copies 755 00:37:30,520 --> 00:37:32,630 of this program in parallel. 756 00:37:32,630 --> 00:37:36,070 So it'll run the first program and the second program in parallel. 757 00:37:36,070 --> 00:37:39,060 So now if I press Enter, it's going to make that request pretty much 758 00:37:39,060 --> 00:37:40,570 instantaneously at the same time. 759 00:37:40,570 --> 00:37:42,620 So let's give this a shot. 760 00:37:42,620 --> 00:37:44,950 >> So now notice it says two processes. 761 00:37:44,950 --> 00:37:50,630 And if you're curious, that 27,000 number is basically the process ID. 762 00:37:50,630 --> 00:37:52,940 And then notice, they printed hey at the same time. 763 00:37:52,940 --> 00:37:56,820 It wasn't like we had to wait five seconds for one and then after that, 764 00:37:56,820 --> 00:37:59,640 five seconds later get the second. 765 00:37:59,640 --> 00:38:03,300 So that's kind of, in some ways, it's not really evidence, but it's 766 00:38:03,300 --> 00:38:07,390 intuitive evidence that it's not just like waiting five seconds and blocking 767 00:38:07,390 --> 00:38:08,960 the entire thread. 768 00:38:08,960 --> 00:38:10,720 OK cool. 769 00:38:10,720 --> 00:38:17,850 >> So Aaron asked a question earlier that was, well what if we do do something-- 770 00:38:17,850 --> 00:38:18,735 Yep? 771 00:38:18,735 --> 00:38:19,051 >> AUDIENCE: Wait. 772 00:38:19,051 --> 00:38:21,585 How is that different from printf buffer, though? 773 00:38:21,585 --> 00:38:23,010 Doesn't it automatically do that? 774 00:38:23,010 --> 00:38:25,390 Why do we have to worry about it? 775 00:38:25,390 --> 00:38:26,555 >> KEVIN SCHMID: Oh, could you say that one more time? 776 00:38:26,555 --> 00:38:29,510 >> AUDIENCE: Doesn't like printf buffer do the exact same thing? 777 00:38:29,510 --> 00:38:31,280 >> KEVIN SCHMID: The printf buffer? 778 00:38:31,280 --> 00:38:32,753 >> AUDIENCE: Yeah. 779 00:38:32,753 --> 00:38:33,244 OK. 780 00:38:33,244 --> 00:38:40,609 Wasn't in one of the quizzes they were talking about how if you right printf 781 00:38:40,609 --> 00:38:42,899 something and then have it pause one second, and then you have it loop ten 782 00:38:42,899 --> 00:38:46,530 times, it'll wait ten seconds and then printf everything together? 783 00:38:46,530 --> 00:38:47,460 >> KEVIN SCHMID: Oh, OK. 784 00:38:47,460 --> 00:38:49,170 >> AUDIENCE: Is it doing the same thing then in this case? 785 00:38:49,170 --> 00:38:52,630 >> KEVIN SCHMID: So the question was basically in one of the former quizzes 786 00:38:52,630 --> 00:38:57,200 or something, there was a question that basically if you say print f 10 787 00:38:57,200 --> 00:39:01,490 things at a time and then slept like in the process of printing those out, 788 00:39:01,490 --> 00:39:04,850 at the end for some reason, it would just dump those all out on the screen. 789 00:39:04,850 --> 00:39:06,740 So there's kind of two different concepts here. 790 00:39:06,740 --> 00:39:10,400 >> So I guess one thing is that, in this case, we're dealing with two different 791 00:39:10,400 --> 00:39:13,510 sort of people asking the server for things at the same time. 792 00:39:13,510 --> 00:39:17,455 And the reason that the printf kind of waits like that and dumps it all out 793 00:39:17,455 --> 00:39:21,760 at once is more related to how printf kind of-- 794 00:39:21,760 --> 00:39:25,100 so the way printf is actually implemented is it basically has to 795 00:39:25,100 --> 00:39:28,850 talk to the operating system to write that stuff to the console. 796 00:39:28,850 --> 00:39:33,460 So it doesn't want to do all of that stuff immediately when you say printf 797 00:39:33,460 --> 00:39:36,260 some string because that could get expensive if it has to 798 00:39:36,260 --> 00:39:37,340 do that every time. 799 00:39:37,340 --> 00:39:41,530 >> So if you do printf hey, your program might not actually print that 800 00:39:41,530 --> 00:39:43,040 immediately to the console. 801 00:39:43,040 --> 00:39:46,080 It might say, OK, I wrote it. 802 00:39:46,080 --> 00:39:49,570 And then kind of wait for you to give it a little more before actually 803 00:39:49,570 --> 00:39:51,380 writing it out to the console. 804 00:39:51,380 --> 00:39:54,040 >> So the reason that that was the case-- 805 00:39:54,040 --> 00:39:56,450 and it's kind of unrelated to the sleep-- 806 00:39:56,450 --> 00:40:00,060 is that the sleep was sort of just injected in there to demonstrate the 807 00:40:00,060 --> 00:40:02,480 fact that it doesn't write it synchronously. 808 00:40:02,480 --> 00:40:06,210 But the reason for that is just performance so that you don't have to 809 00:40:06,210 --> 00:40:08,920 make that many contacts to the operating system. 810 00:40:08,920 --> 00:40:12,300 >> But here, what we're really trying to do with this sleep thing is just show 811 00:40:12,300 --> 00:40:17,730 that when we have two people visiting this website, it's not going to put 812 00:40:17,730 --> 00:40:22,310 them in a line where it's going to say I have to help you, and then when I'm 813 00:40:22,310 --> 00:40:25,350 totally finished helping you after these five seconds, then I'm going to 814 00:40:25,350 --> 00:40:26,750 move onto the next person. 815 00:40:26,750 --> 00:40:29,515 So the first person's request doesn't tie up that event loop 816 00:40:29,515 --> 00:40:31,610 if that makes sense. 817 00:40:31,610 --> 00:40:34,980 >> But here is actually an example of something that will tie 818 00:40:34,980 --> 00:40:36,090 up the event loop. 819 00:40:36,090 --> 00:40:39,130 So here's a horrible function to compute the nth Fibonacci. 820 00:40:39,130 --> 00:40:43,510 It's literally the worse way you can compute the nth Fibonacci number. 821 00:40:43,510 --> 00:40:48,480 And this is actually just to acknowledge where this came from, 822 00:40:48,480 --> 00:40:49,330 there's actually-- 823 00:40:49,330 --> 00:40:53,230 I mean, you can try to go find it-- but there's like a very lengthy blog 824 00:40:53,230 --> 00:40:54,130 post that somebody wrote. 825 00:40:54,130 --> 00:40:55,660 It's like one of those Reddit things. 826 00:40:55,660 --> 00:40:59,650 But somebody criticized Node.js, and they used this as an example. 827 00:40:59,650 --> 00:41:03,700 So I kind of wanted to just show you two different perspectives just to get 828 00:41:03,700 --> 00:41:07,320 a general understanding of the concepts behind these two things. 829 00:41:07,320 --> 00:41:13,050 >> But this is chosen as just a horrible, horribly inefficient computationally 830 00:41:13,050 --> 00:41:15,940 intensive way to compute the nth Fibonacci number. 831 00:41:15,940 --> 00:41:21,180 So just as a side note, why is it horrible like in one way? 832 00:41:21,180 --> 00:41:23,210 Yep? 833 00:41:23,210 --> 00:41:25,130 >> AUDIENCE: Say you start out with 1,000. 834 00:41:25,130 --> 00:41:27,050 1,000 splits into 999 and 998. 835 00:41:27,050 --> 00:41:28,970 Each of this splits into two things. 836 00:41:28,970 --> 00:41:30,890 Each of this splits into two things. 837 00:41:30,890 --> 00:41:31,355 >> KEVIN SCHMID: Right. 838 00:41:31,355 --> 00:41:32,400 >> AUDIENCE: All the way down. 839 00:41:32,400 --> 00:41:32,840 >> KEVIN SCHMID: Exactly. 840 00:41:32,840 --> 00:41:37,330 So just to repeat for the camera, if I call fib on like 1,000 or something 841 00:41:37,330 --> 00:41:41,810 like that, it's obviously not less than or equal to one so I'm going to 842 00:41:41,810 --> 00:41:46,040 go to this else case, and then I'm going to call fib 999 plus fib 998. 843 00:41:46,040 --> 00:41:50,290 And then pretty much all of that work that fib 999 does is 844 00:41:50,290 --> 00:41:52,950 kind of at this level. 845 00:41:52,950 --> 00:41:55,620 If you go down, it's even more redundant than that, but if you just 846 00:41:55,620 --> 00:42:01,390 think computing fib 998 gets us pretty close to fib 999. 847 00:42:01,390 --> 00:42:05,860 So we should really be a little more clever about how we kind of reuse 848 00:42:05,860 --> 00:42:07,580 these, but we're not reusing these things at all. 849 00:42:07,580 --> 00:42:11,860 So you can imagine this gigantic, gigantic tree that's just horrible. 850 00:42:11,860 --> 00:42:12,940 >> But anyway, OK. 851 00:42:12,940 --> 00:42:14,040 So that was fib. 852 00:42:14,040 --> 00:42:15,530 It just takes a while to run. 853 00:42:15,530 --> 00:42:16,510 Yep? 854 00:42:16,510 --> 00:42:17,760 >> AUDIENCE: [INAUDIBLE]. 855 00:42:17,760 --> 00:42:20,430 856 00:42:20,430 --> 00:42:22,481 >> KEVIN SCHMID: Oh, could you repeat the question? 857 00:42:22,481 --> 00:42:23,731 >> AUDIENCE: [INAUDIBLE]. 858 00:42:23,731 --> 00:42:30,840 859 00:42:30,840 --> 00:42:33,990 >> KEVIN SCHMID: Oh so this is just code that's going to be sort of on the 860 00:42:33,990 --> 00:42:34,850 server side. 861 00:42:34,850 --> 00:42:38,100 So this is not going to be found in the browser or anything. 862 00:42:38,100 --> 00:42:42,160 It's basically what we have is that when the user here pretty much makes 863 00:42:42,160 --> 00:42:45,300 their request again, when we sort of make a request, we're going to call 864 00:42:45,300 --> 00:42:47,040 this function on the server side. 865 00:42:47,040 --> 00:42:50,480 And then we'll get the result back from calling that function. 866 00:42:50,480 --> 00:42:52,460 And then we'll just print it to the user. 867 00:42:52,460 --> 00:42:54,580 So the user doesn't really deal with this function too much. 868 00:42:54,580 --> 00:42:57,270 >> Was that the question? 869 00:42:57,270 --> 00:42:58,470 Does that make sense? 870 00:42:58,470 --> 00:42:58,810 OK. 871 00:42:58,810 --> 00:43:00,930 Cool. 872 00:43:00,930 --> 00:43:04,240 >> So again, we do this whole res.writeHead thing where we print out 873 00:43:04,240 --> 00:43:05,150 the header. 874 00:43:05,150 --> 00:43:09,730 And then I end the response by doing the magic number is fib 45. 875 00:43:09,730 --> 00:43:12,100 So let's just run this server. 876 00:43:12,100 --> 00:43:15,190 So I'm going to do a Node fib.js. 877 00:43:15,190 --> 00:43:17,340 So now my fib server is running. 878 00:43:17,340 --> 00:43:21,790 >> And then here, I'm going to do one of these. 879 00:43:21,790 --> 00:43:22,200 OK? 880 00:43:22,200 --> 00:43:24,960 So I'm just going to say, Curl. 881 00:43:24,960 --> 00:43:32,226 So it's going to take a little while but hopefully soon it will finish and 882 00:43:32,226 --> 00:43:35,660 it will print out that 45th Fibonacci number. 883 00:43:35,660 --> 00:43:36,910 >> AUDIENCE: [INAUDIBLE]. 884 00:43:36,910 --> 00:43:40,720 885 00:43:40,720 --> 00:43:42,050 >> KEVIN SCHMID: It should get done pretty soon. 886 00:43:42,050 --> 00:43:43,780 So it should take five to six seconds. 887 00:43:43,780 --> 00:43:48,570 I don't know that's just V8 being super fast, but in any case, this is a 888 00:43:48,570 --> 00:43:52,700 very short example and purposely inelegant of a non-trivial 889 00:43:52,700 --> 00:43:54,200 computation. 890 00:43:54,200 --> 00:43:56,700 So after a while, it does get this. 891 00:43:56,700 --> 00:44:02,130 >> But now, what if I do that same kind of experiment as before where I make 892 00:44:02,130 --> 00:44:04,010 two requests at the same time? 893 00:44:04,010 --> 00:44:06,990 So here I'm going to a Curl on that address, and I'm going 894 00:44:06,990 --> 00:44:08,260 to do another Curl. 895 00:44:08,260 --> 00:44:12,070 And remember, when we did this for the sleep server, when we basically had it 896 00:44:12,070 --> 00:44:15,320 after five seconds, they pretty much both came back right 897 00:44:15,320 --> 00:44:16,380 around the same time. 898 00:44:16,380 --> 00:44:18,650 So it wasn't particularly tied up. 899 00:44:18,650 --> 00:44:20,290 >> But let's try it now. 900 00:44:20,290 --> 00:44:22,370 OK, so we got our two processes. 901 00:44:22,370 --> 00:44:24,210 Remember those are the process IDs. 902 00:44:24,210 --> 00:44:25,900 This is going to be a little awkward while we stall. 903 00:44:25,900 --> 00:44:31,340 904 00:44:31,340 --> 00:44:35,360 So let's just stay here and wait. 905 00:44:35,360 --> 00:44:37,090 >> So one of them should come back after like-- 906 00:44:37,090 --> 00:44:39,190 OK, so one came back. 907 00:44:39,190 --> 00:44:43,570 But then why didn't the second one come back just yet? 908 00:44:43,570 --> 00:44:44,389 Yep? 909 00:44:44,389 --> 00:44:49,280 >> AUDIENCE: The server can't do anything while it's computing that big number. 910 00:44:49,280 --> 00:44:51,620 >> KEVIN SCHMID: Right. 911 00:44:51,620 --> 00:44:55,395 So the response was just that the server really can't do anything while 912 00:44:55,395 --> 00:44:57,460 it's computing that Fibonacci number. 913 00:44:57,460 --> 00:44:59,360 So now I just got my two things back. 914 00:44:59,360 --> 00:45:03,210 But I guess just to think about the code a little more, how it's working 915 00:45:03,210 --> 00:45:05,030 and everything. 916 00:45:05,030 --> 00:45:11,750 >> So this function here is the code that I've told this server to run when it 917 00:45:11,750 --> 00:45:14,100 receives a new incoming request. 918 00:45:14,100 --> 00:45:17,940 So it's just going to run through this entire code, and then it's going to go 919 00:45:17,940 --> 00:45:21,120 back to the event loop and then continue checking for new events. 920 00:45:21,120 --> 00:45:23,670 So basically what we have happening is the server is 921 00:45:23,670 --> 00:45:25,080 listening for new things. 922 00:45:25,080 --> 00:45:28,070 The first person asks for what 45 is. 923 00:45:28,070 --> 00:45:30,490 We run this code to compute it. 924 00:45:30,490 --> 00:45:33,420 This code takes roughly five to six seconds to run. 925 00:45:33,420 --> 00:45:36,990 Then we go back to the event loop and check for new requests. 926 00:45:36,990 --> 00:45:42,700 >> So this is an example of how, if you have things that are so-called compute 927 00:45:42,700 --> 00:45:48,140 bound, or use a lot of computational, not power, but like are 928 00:45:48,140 --> 00:45:50,260 computationally intensive-- 929 00:45:50,260 --> 00:45:54,080 I guess one thing to say about this is that this function is doing totally, 930 00:45:54,080 --> 00:45:56,310 for the most part, pretty useful work right. 931 00:45:56,310 --> 00:45:59,970 The entire time that that callback function was running, it was pretty 932 00:45:59,970 --> 00:46:03,670 much spending most of its time just computing that nth Fibonacci number. 933 00:46:03,670 --> 00:46:05,930 >> But we only had one thread to deal with. 934 00:46:05,930 --> 00:46:10,270 In the Apache model, when two people made the request to get fib 45, we 935 00:46:10,270 --> 00:46:11,610 would have had two different threads. 936 00:46:11,610 --> 00:46:15,060 And then the operating system's job would have been, or the user level 937 00:46:15,060 --> 00:46:18,660 code that manages the threads, would've been to slice that up on the 938 00:46:18,660 --> 00:46:23,060 CPU, or even if you had multiple CPUs, distribute them evenly across the CPUs 939 00:46:23,060 --> 00:46:26,130 so that they were both finish roughly at the same time. 940 00:46:26,130 --> 00:46:31,120 >> So just to show you how we can sort of-- and this is not a total perfect 941 00:46:31,120 --> 00:46:34,280 solution, but sort of how we can make a come back here and do 942 00:46:34,280 --> 00:46:35,880 a little bit better. 943 00:46:35,880 --> 00:46:41,540 So what I have here is a program called Fib C. And this basically uses 944 00:46:41,540 --> 00:46:45,690 another one of Node's modules called The Child Process Module. 945 00:46:45,690 --> 00:46:49,210 So I've included that at the top kind of like I would do a pound include 946 00:46:49,210 --> 00:46:51,230 child process.h or something. 947 00:46:51,230 --> 00:46:56,210 Now I have access to this CP variable which has all my functionality. 948 00:46:56,210 --> 00:47:01,810 >> So now what I'm doing in this response handler is I'm running this program 949 00:47:01,810 --> 00:47:04,100 dot slash fib 45. 950 00:47:04,100 --> 00:47:06,820 So what I've done-- and I'm just going to step out of this program for a 951 00:47:06,820 --> 00:47:07,620 little bit-- 952 00:47:07,620 --> 00:47:11,060 is I've written a C program that basically computes the 953 00:47:11,060 --> 00:47:12,630 nth Fibonacci number. 954 00:47:12,630 --> 00:47:15,960 So here's just a program I've written in C that computes this. 955 00:47:15,960 --> 00:47:19,040 I can compile it, and I can run it at the command line. 956 00:47:19,040 --> 00:47:22,150 And it's going to compute the 45th Fibonacci number. 957 00:47:22,150 --> 00:47:24,510 >> So notice it just takes pretty much as long. 958 00:47:24,510 --> 00:47:28,240 I probably could have used dash 03 to optimize it or something like that, 959 00:47:28,240 --> 00:47:31,050 but I just did like regular compiler settings. 960 00:47:31,050 --> 00:47:33,260 And it prints it out. 961 00:47:33,260 --> 00:47:36,830 >> But now, what am I kind of doing? 962 00:47:36,830 --> 00:47:39,800 Oh sorry, wrong file. 963 00:47:39,800 --> 00:47:42,800 So I do the same stuff with the header as before. 964 00:47:42,800 --> 00:47:45,780 Then I do this cp.exec. 965 00:47:45,780 --> 00:47:49,460 So what this is going to do is it's going to run this program. 966 00:47:49,460 --> 00:47:51,960 But the way this works is that it's not going to wait for 967 00:47:51,960 --> 00:47:53,400 that program to finish. 968 00:47:53,400 --> 00:47:56,000 It just basically says execute this program. 969 00:47:56,000 --> 00:47:58,700 So basically type this into the command prompt kind of. 970 00:47:58,700 --> 00:48:02,350 >> And then, when you're done with it, run this function. 971 00:48:02,350 --> 00:48:04,800 So now we kind of get this whole restored thing of 972 00:48:04,800 --> 00:48:06,750 like we're not waiting. 973 00:48:06,750 --> 00:48:08,530 Does that kind of make sense? 974 00:48:08,530 --> 00:48:08,990 Yep? 975 00:48:08,990 --> 00:48:11,060 >> AUDIENCE: [INAUDIBLE]? 976 00:48:11,060 --> 00:48:13,610 >> KEVIN SCHMID: So this will actually open up a new process to do it. 977 00:48:13,610 --> 00:48:19,310 So this is actually, in some ways, evil, not super evil, but it is 978 00:48:19,310 --> 00:48:22,990 important to say that this is kind of going back to, on one hand, the Apache 979 00:48:22,990 --> 00:48:26,450 model where we do threads and processes for each request or 980 00:48:26,450 --> 00:48:29,280 processes for each request. 981 00:48:29,280 --> 00:48:32,090 So this is kind of analogous to what Apache does. 982 00:48:32,090 --> 00:48:35,110 In some cases, it will just use a new thread, which is a little more light 983 00:48:35,110 --> 00:48:39,040 weight than a process, but Apache could end up forking a new process 984 00:48:39,040 --> 00:48:43,370 which is kind of what we do here implicitly by doing dot slash fib 45. 985 00:48:43,370 --> 00:48:48,690 >> And then in that case, we kind of incur the same expenses of processes. 986 00:48:48,690 --> 00:48:50,710 So this is just one thing you can do. 987 00:48:50,710 --> 00:48:52,600 But just to show this sort of running. 988 00:48:52,600 --> 00:48:57,120 And this talk is just really aimed at presenting these kind of programs as a 989 00:48:57,120 --> 00:49:00,710 way to show different perspectives on how to design servers like that. 990 00:49:00,710 --> 00:49:08,300 So this is running, and then now if I do this again, I got two process IDs. 991 00:49:08,300 --> 00:49:10,320 >> Let's just talk about things to point out. 992 00:49:10,320 --> 00:49:11,980 So notice that they're incrementally. 993 00:49:11,980 --> 00:49:12,700 That's cool. 994 00:49:12,700 --> 00:49:15,140 Because it was 27,122 before. 995 00:49:15,140 --> 00:49:18,580 But notice now, they came back at roughly the same time. 996 00:49:18,580 --> 00:49:23,960 >> And now, a good question to ask about why was that the case is, whose job 997 00:49:23,960 --> 00:49:27,590 was it now to sort of make these things kind of play fair with each 998 00:49:27,590 --> 00:49:31,350 other, these two instances of dot slash fib 45 that I ran 999 00:49:31,350 --> 00:49:32,880 or that Node ran? 1000 00:49:32,880 --> 00:49:36,940 Who sort of makes it fair that they both get kind of balanced run time? 1001 00:49:36,940 --> 00:49:42,900 1002 00:49:42,900 --> 00:49:44,220 >> AUDIENCE: [INAUDIBLE]. 1003 00:49:44,220 --> 00:49:44,620 >> KEVIN SCHMID: Yeah. 1004 00:49:44,620 --> 00:49:49,740 So basically, when I do dot slash fib 45 or something like that, now it's 1005 00:49:49,740 --> 00:49:53,750 kind of up to the operating system to handle the runtime of those programs. 1006 00:49:53,750 --> 00:49:56,920 And now it can schedule them on different CPUs or it 1007 00:49:56,920 --> 00:49:58,330 can schedule them. 1008 00:49:58,330 --> 00:50:01,870 It can slice up the time that one CPU gets it or that they get 1009 00:50:01,870 --> 00:50:03,330 to run on one CPU. 1010 00:50:03,330 --> 00:50:05,620 So that's the idea behind that. 1011 00:50:05,620 --> 00:50:07,220 Does that make sense to everybody? 1012 00:50:07,220 --> 00:50:10,950 So now Node isn't really playing a part in dividing up these tasks. 1013 00:50:10,950 --> 00:50:12,200 OK. 1014 00:50:12,200 --> 00:50:14,050 1015 00:50:14,050 --> 00:50:17,840 >> So that's almost it for examples. 1016 00:50:17,840 --> 00:50:23,370 I just wanted to show one more thing because a lot of this so far has been 1017 00:50:23,370 --> 00:50:27,350 not totally super practical in some cases. 1018 00:50:27,350 --> 00:50:30,970 I can imagine coming home after this talk and something and saying like, 1019 00:50:30,970 --> 00:50:35,710 well I kind of got out of that talk that I can make a Fibonacci server for 1020 00:50:35,710 --> 00:50:37,360 my final project. 1021 00:50:37,360 --> 00:50:40,770 So here's just sort of one more example that hopefully will be-- 1022 00:50:40,770 --> 00:50:44,620 maybe not, but maybe-- a little more sort of relevant to final projects and 1023 00:50:44,620 --> 00:50:46,440 thinking ahead for things like that. 1024 00:50:46,440 --> 00:50:48,870 >> So this is chat.js. 1025 00:50:48,870 --> 00:50:52,290 So this is kind of like some sample server side code that you could use to 1026 00:50:52,290 --> 00:50:55,700 set up a small chat server like you may have seen on the 1027 00:50:55,700 --> 00:50:56,630 Facebook Chat or whatever. 1028 00:50:56,630 --> 00:50:59,990 So I'm not saying this is like Facebook Chat, but this is kind of 1029 00:50:59,990 --> 00:51:06,230 like a good-- maybe not good, but maybe good-- starting point for a chat 1030 00:51:06,230 --> 00:51:08,560 server for your website for a final project. 1031 00:51:08,560 --> 00:51:11,040 So let's look at what it's doing. 1032 00:51:11,040 --> 00:51:15,500 >> So we're getting this special thing at the top, this var SIO 1033 00:51:15,500 --> 00:51:17,050 equals require Socket.IO. 1034 00:51:17,050 --> 00:51:20,280 So this is another thing that it doesn't actually come bundled with 1035 00:51:20,280 --> 00:51:22,300 Node but you can install it. 1036 00:51:22,300 --> 00:51:23,480 It's a Node module. 1037 00:51:23,480 --> 00:51:25,670 So it's just like some extension to Node. 1038 00:51:25,670 --> 00:51:29,220 >> SocketIO is actually really kind of cool. 1039 00:51:29,220 --> 00:51:32,550 It's an abstraction that basically what it does is is it allows you to 1040 00:51:32,550 --> 00:51:35,770 have this stream of communication between a web 1041 00:51:35,770 --> 00:51:39,430 browser and a web server. 1042 00:51:39,430 --> 00:51:45,250 So for the most part so far, we've had these very quick one second or two 1043 00:51:45,250 --> 00:51:48,790 second communications between a web browser and the web server. 1044 00:51:48,790 --> 00:51:53,310 So it's basically go to google.com, get the stuff, send it back, and then 1045 00:51:53,310 --> 00:51:53,770 we're done. 1046 00:51:53,770 --> 00:51:56,560 We're never talking again until the user types in something else. 1047 00:51:56,560 --> 00:52:01,090 >> But what Socket.IO and similar kind of things-- and SocketIO is actually one 1048 00:52:01,090 --> 00:52:03,940 of the things that is built on as WebSocket which is sort of available 1049 00:52:03,940 --> 00:52:06,440 as part of HTML5-- 1050 00:52:06,440 --> 00:52:09,620 that allows you to have this continuing dialogue. 1051 00:52:09,620 --> 00:52:13,990 And this is very useful in a chat server kind of thing because it is 1052 00:52:13,990 --> 00:52:17,390 kind of like a continuing dialogue in some ways because if you're chatting 1053 00:52:17,390 --> 00:52:21,540 with somebody, you can now just send a message down the pipe, and then the 1054 00:52:21,540 --> 00:52:23,940 server can send a message down the pipe to the other person you're 1055 00:52:23,940 --> 00:52:24,520 chatting with. 1056 00:52:24,520 --> 00:52:26,903 And then you can have this exchange like that. 1057 00:52:26,903 --> 00:52:30,590 1058 00:52:30,590 --> 00:52:32,850 >> So that's kind of what SocketIO is good for. 1059 00:52:32,850 --> 00:52:38,400 The reason that SocketIO uses WebSockets as one thing is that in 1060 00:52:38,400 --> 00:52:43,980 addition to just plain old WebSockets, it also does some tricks to basically 1061 00:52:43,980 --> 00:52:45,610 make it browser compatible. 1062 00:52:45,610 --> 00:52:50,040 So browsers like Internet Explorer unfortunately don't support WebSockets 1063 00:52:50,040 --> 00:52:50,810 right out of the box. 1064 00:52:50,810 --> 00:52:55,290 So it uses some other kind of cool neat things with Adobe Flash to allow 1065 00:52:55,290 --> 00:52:57,170 you to have cross browser support. 1066 00:52:57,170 --> 00:52:58,800 So that's really useful. 1067 00:52:58,800 --> 00:53:02,440 >> And actually, I know I'm kind of running on time here, but CS50 1068 00:53:02,440 --> 00:53:08,490 Discuss, have you ever seen something like, I don't know, blank so and so is 1069 00:53:08,490 --> 00:53:11,030 replying to this post or something like that, that feature? 1070 00:53:11,030 --> 00:53:12,250 That's SocketIO. 1071 00:53:12,250 --> 00:53:15,580 So when somebody starts typing in the discuss box to make a reply or 1072 00:53:15,580 --> 00:53:20,040 something, your browser does what's called in SocketIO emits some kind of 1073 00:53:20,040 --> 00:53:22,660 event that says somebody's replying to this post. 1074 00:53:22,660 --> 00:53:26,110 >> Then the server says, OK, what do I have to do? 1075 00:53:26,110 --> 00:53:29,270 Well now I have to tell those other guys who are on CS50 Discuss looking 1076 00:53:29,270 --> 00:53:31,170 at this post that somebody's replying. 1077 00:53:31,170 --> 00:53:34,190 So that's kind of what SocketIO is good for, this continuing kind of 1078 00:53:34,190 --> 00:53:35,930 stream of dialogue. 1079 00:53:35,930 --> 00:53:37,250 OK. 1080 00:53:37,250 --> 00:53:40,060 >> So what I have here-- and we're just going to ignore the connections array 1081 00:53:40,060 --> 00:53:41,390 for a little bit-- 1082 00:53:41,390 --> 00:53:43,080 what I do is I do another listen. 1083 00:53:43,080 --> 00:53:46,880 So that's just the way in Socket.IO is saying let's listen on this port. 1084 00:53:46,880 --> 00:53:50,060 And then I do this on connection. 1085 00:53:50,060 --> 00:53:53,360 So that's just basically Socket IO's way of saying, when we receive a 1086 00:53:53,360 --> 00:53:55,840 connection, I want you to run this code. 1087 00:53:55,840 --> 00:53:59,870 >> And notice that instead of having rec and res passed in there I have Socket. 1088 00:53:59,870 --> 00:54:03,260 And this Socket idea is basically this thing that you can write to and read 1089 00:54:03,260 --> 00:54:05,750 from that has the user's messages possibly. 1090 00:54:05,750 --> 00:54:10,700 And the messages that you would send can go through that Socket. 1091 00:54:10,700 --> 00:54:12,140 Does that make sense? 1092 00:54:12,140 --> 00:54:14,490 So it's this continuing thing. 1093 00:54:14,490 --> 00:54:17,820 >> So what I do is I call Socket.emit. 1094 00:54:17,820 --> 00:54:20,330 And emit takes pretty much two arguments. 1095 00:54:20,330 --> 00:54:24,100 The first argument is a string just representing the type of 1096 00:54:24,100 --> 00:54:25,270 thing you're emitting. 1097 00:54:25,270 --> 00:54:28,120 So for this case, I've use this string new message. 1098 00:54:28,120 --> 00:54:32,670 And that's just basically saying that the type of this thing, what I'm 1099 00:54:32,670 --> 00:54:34,750 sending, is a new message. 1100 00:54:34,750 --> 00:54:38,460 So you can listen for specific types like new message or whatever 1101 00:54:38,460 --> 00:54:39,960 by using dot on. 1102 00:54:39,960 --> 00:54:44,570 >> So connection and user sent there, if you look at where we call dot on, 1103 00:54:44,570 --> 00:54:48,150 those are other strings that represent types of user messages. 1104 00:54:48,150 --> 00:54:52,060 So it's basically you can have this emit one of these message types, and 1105 00:54:52,060 --> 00:54:55,520 then do something in response to one of these message types 1106 00:54:55,520 --> 00:54:57,640 >> So I'm emitting this new message. 1107 00:54:57,640 --> 00:55:00,540 We're going to ignore connections.push for a second. 1108 00:55:00,540 --> 00:55:03,360 But then I say, Socket.on user sent. 1109 00:55:03,360 --> 00:55:07,540 So now it's kind of like when the user sends me a message, I want 1110 00:55:07,540 --> 00:55:09,240 you to run this code. 1111 00:55:09,240 --> 00:55:12,080 And notice that that anonymous function is taking in this variable 1112 00:55:12,080 --> 00:55:16,300 called data which is basically going to have the user's message. 1113 00:55:16,300 --> 00:55:20,700 >> So now let's kind of talk about the connections array. 1114 00:55:20,700 --> 00:55:24,590 So this is designed for a chat client where basically everybody's kind of in 1115 00:55:24,590 --> 00:55:25,950 the same chat room. 1116 00:55:25,950 --> 00:55:29,640 So basically, what we need to keep around is some array that basically 1117 00:55:29,640 --> 00:55:33,170 represents all the people chatting in some ways, if that makes sense. 1118 00:55:33,170 --> 00:55:33,340 Right? 1119 00:55:33,340 --> 00:55:37,190 Because we need to know who those guys are so we can send them the messages 1120 00:55:37,190 --> 00:55:39,140 that other people send to us. 1121 00:55:39,140 --> 00:55:44,440 >> So what this code does is when user sends a message-- that's the type of 1122 00:55:44,440 --> 00:55:45,300 the event-- 1123 00:55:45,300 --> 00:55:47,120 we're going to run this code. 1124 00:55:47,120 --> 00:55:51,240 And what we do is we run through this array that we have called connections. 1125 00:55:51,240 --> 00:55:54,390 And pretty much for every connection except the one that's ours, that's 1126 00:55:54,390 --> 00:55:59,520 what this code says, we send a new message with that attached message 1127 00:55:59,520 --> 00:56:01,210 information. 1128 00:56:01,210 --> 00:56:04,880 >> So if you notice here, what I did when the user actually makes a new 1129 00:56:04,880 --> 00:56:08,560 connection is I've added with the JavaScript.push method, this is 1130 00:56:08,560 --> 00:56:12,100 basically just saying like add that Socket as a value into 1131 00:56:12,100 --> 00:56:13,900 our connections array. 1132 00:56:13,900 --> 00:56:20,560 So now when this code runs, it will send things to those particular 1133 00:56:20,560 --> 00:56:22,020 connections. 1134 00:56:22,020 --> 00:56:26,980 So this can be a good starting point for making a chat server 1135 00:56:26,980 --> 00:56:28,250 or something similar. 1136 00:56:28,250 --> 00:56:33,315 >> And the kind of cool thing is that the code that you see here for like on and 1137 00:56:33,315 --> 00:56:36,390 emit and stuff like that is the same kind of JavaScript code that you would 1138 00:56:36,390 --> 00:56:39,260 write in the browser to interact with the server. 1139 00:56:39,260 --> 00:56:42,480 So that's why SocketIO is kind of neat and useful in that way. 1140 00:56:42,480 --> 00:56:45,680 1141 00:56:45,680 --> 00:56:49,250 >> Oh and just one more thing real quick. 1142 00:56:49,250 --> 00:56:55,970 There was a CS50 final project last year that basically implemented a chat 1143 00:56:55,970 --> 00:56:57,840 server in Node.js. 1144 00:56:57,840 --> 00:57:01,350 I think it's Harvardchats.org but I'm not-- 1145 00:57:01,350 --> 00:57:02,940 OK. 1146 00:57:02,940 --> 00:57:06,190 I'm not sure what the URL is, but I can send that out afterwards. 1147 00:57:06,190 --> 00:57:09,260 But it's kind of cool what you can do with Node.js. 1148 00:57:09,260 --> 00:57:14,680 >> So I hope, in general, you guys have a good sense of what Node.js is useful 1149 00:57:14,680 --> 00:57:17,870 for and how you could maybe apply to your final project. 1150 00:57:17,870 --> 00:57:21,050 I will be sending out some more resources along with this. 1151 00:57:21,050 --> 00:57:23,170 And thank you for coming. 1152 00:57:23,170 --> 00:57:23,610 Thank you. 1153 00:57:23,610 --> 00:57:27,168 >> [APPLAUSE]