Node

Node.js

Mendel Rosenblum

CS142 Lecture Notes - Node.js

Threads

versus

Events

request = readRequest(socket);

startRequest(socket);

reply = processRequest(request);

listen("requestAvail", processRequest);

sendReply(socket, reply);

listen("processDone", sendReplyToSock);

Implementation:

Implementation:

Thread switching (i.e. blocking) and a

Event queue processing

scheduler

CS142 Lecture Notes - Node.js

Threads versus Events using Callbacks

readRequest(socket, function(request) {

processRequest(request,

function (reply) {

sendReply(socket, reply);

});

});

request = readRequest(socket);

reply = processRequest(request);

sendReply(socket, reply);

Implementation:

Implementation:

Event queue processing

Thread switching (i.e. blocking) and a

scheduler

CS142 Lecture Notes - Node.js

Event queue

¡ñ

Inner loop

while (true) {

if (!eventQueue.notEmpty()) {

eventQueue.pop().call();

}

}

¡ñ

Never wait/block in event handler . Example readRequest(socket);

1.

launchReadRequest(socket);

2.

When read finishes: eventQueue.push(readDoneEventHandler);

// Returns immediately

CS142 Lecture Notes - Node.js

Node.js

¡ñ

Take a JavaScript engine from a browser (Chrome's V8 JavaScript Engine)

¡ð

¡ð

¡ñ

Include DOM-like events and an event queue

¡ð

¡ñ

Everything runs as a call from the event loop (already had one for browser events)

Make event interface to all OS operations

¡ð

¡ð

¡ñ

Get same JavaScript on both browser and server

Don't need the DOM on the server

Wrap all the OS blocking calls (file and socket/network io)

Add some data handle support

Add a proper module system (predated import/export)

¡ð

Each module gets its own scope (not everything in window)

CS142 Lecture Notes - Node.js

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download