Tutor HuntResources Javascript Resources
Using Javascript To Build High-performance Network Programs
Java Script
Date : 15/04/2014
Author Information
Uploaded by : Mohit
Uploaded on : 15/04/2014
Subject : Javascript
Node.js - also called Node - is a serverside
Javascri pt environment (see http://
nodejs.org). It's based on Google's runtime
implementation - the aptly named "V8" engine.
V8 and Node are mostly implemented in C and
C++, focusing on performance and low memory
consumption. But, whereas V8 supports mainly
Javascri pt in the browser (most notably, Google
Chrome), Node aims to support long-running
server processes.
Unlike in most other modern environments,
a Node process doesn't rely on multithreading
to support concurrent execution of business
logic; it's based on an asynchronous I/O eventing
model. Think of the Node server process as a
single-threaded daemon that embeds the Javascri pt
engine to support customization. This is
different from most eventing systems for other
programming languages, which come in the
form of libraries: Node supports the eventing
model at the language level.
Javascri pt is an excellent fit for this approach
because it supports event callbacks. For example,
when a browser completely loads a document,
a user clicks a button, or an Ajax request
is fulfilled, an event triggers a callback. Javascri pt's
functional nature makes it extremely
easy to create anonymous function objects that
you can register as event handlers.
Multithreading versus Events
Application developers who deal with multiple
I/O sources, such as networked servers handling
multiple client connections, have long employed
multithreaded programming techniques. Such
techniques became popular because they let
developers divide their applications into concurrent
cooperating activities. This promised to
not only make program logic easier to understand,
implement, and maintain but also enable
faster, more efficient execution.
For applications such as Web servers performing
significant amounts of I/O, multiple threads
enable applications to better use available processors.
Running multiple concurrent threads on
a modern multicore system is straightforward,
with each core simultaneously executing a different
thread with true parallelism. On single-core
systems, the single processor executes one
thread, switches to another and executes it, and
so on. For example, the processor switches its
execution context to another thread when the
current thread performs an I/O operation, such
as writing to a TCP socket. The switch occurs
because completing that operation can take
many processor cycles. Rather than wasting
cycles waiting for the socket operation to finish,
the processor sets the I/O operation in motion
and executes another thread, thus keeping itself
busy doing useful work. When the I/O operation
ends, the processor again considers the original
thread to be ready to execute because it's no longer
blocked while waiting for I/O.
Even though many developers have successfully
used multithreading in production
applications, most agree that multithreaded
programming is anything but easy. It's fraught
with problems that can be difficult to isolate and
correct, such as deadlock and failure to protect
resources shared among threads. Developers
also lose some degree of control when drawing
on multithreading because the OS typically
decides which thread executes and for how long.
Event-driven programming offers a more
efficient, scalable alternative that provides developers
much more control over switching between
application activities. In this model, the application
relies on event notification facilities such as the select() and poll() Unix system
calls, the Linux epoll service,
and the kqueue and kevent calls
available in BSD Unix variants such
as OS X. Applications register interest
in certain events, such as data being
ready to read on a particular socket.
When the event occurs, the notification
system notifies the application so
that it can handle the event.
Asynchronous I/O is important
for
event-driven programming because
it prevents the application from getting
blocked while waiting in an I/O
operation. For example, if the application
writes to a socket and fills the
socket's underlying buffer, ordinarily,
the socket blocks the application's
writes until buffer space becomes
available, thus preventing the application
from doing any other useful
work. But, if the socket is nonblocking,
it instead returns an indication
to the application that further writing
isn't currently possible, thereby
informing the application that it
should try again later. Assuming the
application has registered interest
with the event notification system in
that socket, it can go do something
else, knowing that it will receive an
event when the socket's write buffer
has available space.
Like multithreaded programming,
event-driven programming
with asynchronous I/O can be
problematic.
One problem is that
not all interprocess-communication
approaches
can be tied into the event
notification facilities we mentioned
earlier. For example, on most OSs,
for two applications to communicate
through shared memory, sharedmemory
segments provide no handles
or file descri ptors enabling the
application to register for events. For
such cases, developers must resort
to alternatives such as writing to
a pipe or some other event-capable
mechanism together with writing to
shared memory.
Another significant problem is
the sheer complexity of writing
applications in certain programming
languages to deal with events and
asynchronous I/O. This is because
different events require different
actions in different contexts. Programs
typically employ callback
functions to deal with events. In
languages that lack anonymous
functions and closures, such as C,
developers must write individual
functions specifically for each event
and event context. Ensuring that
these functions all have access to
the data and context information
they require when they're called to
handle an event can be incredibly
perplexing. Many such applications
end up being little more than impenetrable,
unmaintainable tangles of
spaghetti code and global variables.
This resource was uploaded by: Mohit