1. Introduction to Node.js and My Journey of Learning
Hello, everyone. We're gonna make some crazy experiments today. I tried recreating Node.js from scratch and I'm going to tell you all the step-by-step. First of all, all the slides, all the links, references and even a tutorial for you to follow after this conference I put on the last slides. Mention me, mention the event because this is how you can push our job further. Well, all this talk is about, do I really know Node.js? So I start researching a lot of things and start making questions. I've been creating a bunch of videos doing the same ideas. But this talk, I already have a tutorial there and here today I'm going to show you some highlights, and I even wrote a blog post for you later to extend.
Hello, everyone. How's it been? Yay! Let's try again. How's it been? Whoo! Energy, yes! We're gonna make some crazy experiments today. This is something I love doing, like learning new stuff, and I love doing it by trying it, right?
So I tried recreating Node.js from scratch and I'm going to tell you all the step-by-step in how I've been doing this. First of all, all the slides, all the links, references and even a tutorial for you to follow after this conference I put on the last slides. So not just listen to me here, try at home because you can get a lot of cool other insights. And for sure, take as many pictures as you can because this helps us a lot. Mention me, mention the event because this is how you can push our job further, right?
Well, all this talk is about, I start myself asking myself, do I really know Node.js? So I was looking, I became part of the Node.js car team there and I was so afraid of being in some conference and people asking me questions of how Node.js was working behind the scenes. And I figure, well, I don't know as much as I wish or as much as I thought I would. So I start researching a lot of things and I start making questions and chat dpt has helped a lot to answer those questions. But I've been creating a bunch of videos doing the same ideas. So if you like more math science projects, I've re-implemented the web socket protocol from scratch using binary code, a bunch of cool stuff, and other, creating your code coverage. But this talk, I already have a tutorial there and here today I'm going to show you some highlights, and I even wrote a blog post for you later to extend, because it's just 20 minutes here so you can follow along.
2. Introduction to Node.js and My Journey
I'm not a C++ developer, but I'm curious and eager to learn. This research is my own, and I had to reach out to Google for help. Shout-out to Ben, Dino, Node.js, and the creators for their amazing job. They abstract the complexity, allowing us to create powerful applications. Let's do some magic!
Well, just before we go on, I just want to let you know that I'm not a C++ developer, okay? Some C++ developers on the back are going to see bad practices here, but I'm just a curious person trying to learn something on the hard way, okay? Well, this is part of my own research, so this content you aren't going to see anything on the internet. So it was really hard for me, I had to schedule some calls with people from Google, and if they said, I've never done this in my life, so try it yourself. It was crazy. And just a shout-out for Ben, Dino, Node.js, and all the creators from this runtime. You are going to figure out today how nice and how crazy is their job. Because they are abstracting all the complexity for us so we can use an accessible language to create powerful applications. And let's do some magic, I love this game!
3. Node.js Components and Experimentation
I tried finding GitHub projects, but they only did V8 and never used all the components. I wanted to experiment and went to the Node.js website, but it wasn't enough. I found the first Node.js version on GitHub and realized it was just a proof of concept. Now let's talk about the main components: JS, V8, C++, and libuv. The event loop is the magic in libuv, and the C++ bridge connects everything. If we have a JS file with just 'print', it won't work.
The first thing I did was try to find GitHub projects that were doing it, but they were only doing V8, only one part, and they were never using all the components. So I tried figuring out how could I create all the components using the same thing.
So I've been to the Node.js website, there you can see how Event Loops works, how Node.js works, but for me it wasn't enough, it was a bunch of theory, but I wanted to experiment. I wanted to have my hands dirty and start working on it. Well, the best part, right? We have everything on GitHub. So if you go there, you're going to see the first Node.js version. Has anyone seen this image before, this web page? This is crazy, isn't it? And see, Node.js wasn't using console.log, it was using puts like Ruby on Rails and just a lot of explanation, right? Well, I went to this repo, on the Node.js official repo, but on the V001 and I started browsing all the files and started realizing how Ryan Dahl thought and getting some assumptions, right? Trying to find out some ideas of some years ago. And something you're going to notice, sometimes we see people as our heroes, right? But if you see the commit story, oh, first compile, nothing works. Three dots, what else? Web page, very incomplete. Trying to implement something, not working. I'm not sure about you, but I don't usually commit everything, but see, Node.js was just a proof of concept. It wasn't something that he-he realized was so big.
4. Implementing Functions in V8
5. Implementing setTimeout in Node.js
On the setTimeout side, it's exactly the same thing. I have to create the C++ function now, and then I can use uvtimerstart, just to reference that we are using libuv to create and to call those functions. I'm injecting a function on the global state, now the global disk has the setTimeout, and the V8 now thinks that setTimeout was always there. It's pretty great how Node.js is working. If you want to create your runtime, you've got to focus on creating your own implementations.
Well, doing something harder, setTimeout. On the setTimeout side, it's exactly the same thing. I have to create the C++ function now, and then I can use uvtimerstart, just to reference that we are using libuv to create and to call those functions. But C is exactly the same thing. I'm injecting a function on the global state, now the global disk has the setTimeout, and the V8 now thinks that setTimeout was always there. It's pretty great how Node.js is working. In the end, my reaction was like this, Mom, come here! You know that Node.js is just extending functions, there is no magic behind this. I wouldn't say just, because there are a bunch of things in the end happening, but if you want to create your runtime, you've got to focus on creating your own implementations.
6. Execution Pipeline and Calling console.log
7. Surprises with setTimeout and setInterval
But if I try printing the console, I'm going to see there's an object there. Pretty weird. I was like, why? If I use object keys, I'm going to see all the console API, all the methods, they are there, but they are just not useful. I think it's just an interface for us to implement ourselves. I don't know.
8. Linking Elements with libuv and C++
9. Event Loop and Extending V8
My friends, I cannot tell you how was my feeling when I saw those logs working for the first time. I was calling everyone like, oh my God, it finally worked because it's really hard. All these examples here, you're gonna notice I'm using Gitpod right on the bottom. So all those examples, you won't need to compile it by yourself on your own. You just fork my project there and you're gonna see all the step by step. You can focus only on the part of the code, so I save a lot of problem for you.
Continuing with Node.js and Bun
If you want to see more content, go to my website. I built an ebook for you and put a challenge to implement your own FS red file. Node.js has a great community and continues to evolve. It's still fast compared to newer runtimes. Bun is said to be faster than Node.js, but the programming language they chose is different - Zig.
If you want to see more content, you want to see more of my talks, go to my website. You're gonna see a bunch of cool stuff to learn also from there. And, for sure, I built an ebook for you, teaching by step, and I also put a good challenge for you to implement your own FS red file.
All right. So before I finish this talk, let's take our traditional selfie so we can make people on the other side envy, okay? All right. I'm gonna trust you, right? So I'm gonna count on three, and we do wow, okay? So one, two, three. Hey! Thank you so much for having me.
Exploring Node.js and Writing a Book
I found the time to explore Node.js and write a book because it was a good opportunity for me to learn and create unique content. Despite the delays and challenges, it was worth it in the end. I'm planning to work on a React Native clone, which will be a complex and exciting project.