I\'m just starting to play with Node.js today, and thought I\'d start with what I thought would be a simple script: Connecting to a server via sockets, and sending a bit of
First of all, let's make clear what a EventEmitter
is. JavaScript and therefore Node.js are asynchronous
. That means, instead of having to wait for incoming connections on a server object, you add a listener
to the object and pass it a callback function
, which then, "as soon" as the event happens, gets executed.
There's still waiting here and there going on in the background but that has been abstracted away from you.
Let's take a look at this simple example:
// #1) create a new server object, and pass it a function as the callback
var server = net.createServer(function (stream) {
// #2) register a callback for the 'connect' event
stream.on('connect', function () {
stream.write('hello\r\n'); // as
});
// #3) register a callback for the 'data' event
stream.on('data', function (data) {
stream.write(data);
});
// #4) register a callback for the 'end' event
stream.on('end', function () {
stream.write('goodbye\r\n');
stream.end();
});
});
// #5) make the server listen on localhost:8124
server.listen(8124, 'localhost');
So we create the server and pass it the callback function
, this function is not yet executed. Passing the function here is basically a shortcut for adding a listener for the connection
event of the server object. After that we start the server at #5
.
Now what happens in the case of an incoming connection?
Since the function we passed to createServer
was bound to the connection
event, it now gets executed.
It adds the connect
, data
and end
event listeners to the stream object
(which represents the individual connection) by hooking up callbacks for the events.
After that, the stream
fires the connect
event, therefore the function passed at #2
gets executed and writes hello\r\n
to the stream. How does the function know which stream it should write to? Closures are the answer, the function inherits the scope it was created in, therefore inside the function stream
is still referencing to the individual connection that triggered this very callback we're in right now.
Now the client sends some data over the connection, which makes the stream
object call its data
event, since we bound a function to this event at #3
we now echo the incoming data back to the client.
In case the client closes the connection, the function we've bound at #4
gets called, which writes goodbye\r\n
and after that closes the connection from our side.
Does this make things a little bit more clear? Well it definitely makes the whole thing a lot easier. Node is, just as well as JavaScript is inside Browsers, single threaded
. There's only one thing happening at a given point time.
To describe it simple, all these callbacks
end up in a global queue and are then called one after another, so this queue may(abstracted) look like this:
| connection event for a new stream
| data event for stream #12
| callback set via setTimeout
v close event of yet another stream
These are now get executed top to bottom, nothing will ever happen in between those. There's no chance, that while you're doing something in the callback
bound to the data
event, something will other will happen and magically change the state of the system. Even if there is a new incoming connection on the server, its event will get queued up and it will have to wait until everything before it, including the data
event you're currently in, finishes.