Introduction to npm

This was the third in a series of posts leading up to Node.js Knockout on how to use node.js.

npm is a NodeJS package manager. As its name would imply, you can use it to install node programs. Also, if you use it in development, it makes it easier to specify and link dependencies.

Installing npm

First of all, install NodeJS. Like so much of the NodeJS ecosystem, npm is very young, so you’ll generally have to use a very recent version of node in order to use it. At the time of writing this, that means at least version 0.1.103.

To install npm in one command, you can do this:

curl http://npmjs.org/install.sh | sh 

Continue reading

Advertisements

Hosting node.js applications in IIS on Windows

what is iisnode

iisnode is a native IIS module that allows hosting of node.js applications in IIS on Windows.

benefits

  • Process management. The iisnode module takes care of lifetime management of node.exe processes making it simple to improve overall reliability. You don’t have to implement infrastructure to start, stop, and monitor the processes.
  • Scalability on multi-core servers. Since node.exe is a single threaded process, it only scales to one CPU core. The iisnode module allows creation of multiple node.exe processes per application and load balances the HTTP traffic between them, therefore enabling full utilization of a server’s CPU capacity without requiring additional infrastructure code from an application developer.
  • Auto-update. The iisnode module ensures that whenever the node.js application is updated (i.e. the script file has changed), the node.exe processes are recycled. Ongoing requests are allowed to gracefully finish execution using the old version of the application, while all new requests are dispatched to the new version of the app.
  • Access to logs over HTTP. The iisnode module provides access the output of the node.exe process (e.g. generated by console.log calls) via HTTP. This facility is key in helping you debug node.js applications deployed to remote servers.
  • Side by side with other content types. The iisnode module integrates with IIS in a way that allows a single web site to contain a variety of content types. For example, a single site can contain a node.js application, static HTML and JavaScript files, PHP applications, and ASP.NET applications. This enables choosing the best tools for the job at hand as well progressive migration of existing applications.
  • Minimal changes to node.js application code. The iisnode module enables hosting of existing HTTP node.js applications with very minimal changes. Typically all that is required is to change the listed address of the HTTP server to one provided by the iisnode module via the process.env.PORT environment variable.
  • Integrated management experience. The issnode module is fully integrated with IIS configuration system and uses the same tools and mechanism as other IIS components for configuration and maintenance.
  • Other IIS benefits. Port sharing, security, URL rewriting, compression, caching, logging

prerequisities

building

For 32 bit Windows: msbuild /p:Platform=Win32 src\iisnode\iisnode.sln

For 64 bit Windows: msbuild /p:Platform=x64 src\iisnode\iisnode.sln

installing after build

build\debug\%PROCESSOR_ARCHITECTURE%\install.bat

installing from a download

samples

http://localhost/node (available after installation)

Which binary version do I need to download?

Please choose x86 flavors for installation on 32-bit Windows systems, and amd64 flavors for installation on 64-bit Windows systems.

Does iisnode block threads per request or connection

iisnode is completely async and does not block threads per request or connection. It does allocate small amounts of memory for active requests.

How do I report issues?

Please verify if your issue is already known by searching https://github.com/tjanczuk/iisnode/issues. If you don’t find it there, create a new issue report. Please include the following information with your bug report:

  • Windows OS version and bitness (32/64)
  • IIS version
  • which version of iisnode you are using (changelist number if compiled, binary package name if downloaded)
  • all interesting information from the IIS error page that displays (if any)
  • all interesting information IIS logs into the Event Viewer (http://msdn.microsoft.com/en-us/library/ms524984(v=vs.90).aspx)

I am getting a 503 response when running samples

If you run into a 503 status code with IIS reporting error code 0x5 (ERROR_ACCESS_DENIED), make sure the identity of the application pool running the site that hosts your node.js application has read & execute rights to node.exe on disk. If you are using the default IIS configuration (DefaultAppPool running under ApplicationPoolIdentity), you can set appropriate ACLs on the node.exe by running:

icacls %systemdrive%\node\node.exe /grant IIS_IUSRS:rx

resources & documentation

Setting up a Virtual Machine for Node.js development on Windows 7

If you run Windows 7 and you want to develop for the latest version of Node.js, you’re in for a bumpy ride. You can compile it under Cygwin, but the process is complex and doesn’t work for all versions. So what if:

  • You fear a later version of Node.js breaking on Windows again? (It was broken for two full minor versions, really quite a while.)
  • You don’t want to install Cygwin? (You madman, you really should.)
  • You are paranoid and want to be running your programs on the same environment you will be deploying them on?
  • You just can’t get the dang instructions to work?

Well, the solution isn’t to reformat and install Linux (though that could very easily help your productivity – fewer games being played, I’m sure). The solution is to install Node.js on a Virtual Machine with a shared drive. And honestly? It’s pretty easy. Three steps!

Step One: Install VirtualBox

Download VirtualBox here, then follow the instructions.

Step Two: Install Ubuntu Server

Download Ubuntu Server here, then create a new Virtual Machine. Name it what you will.

Under the “Network” tab in settings, change the adapter to a “Bridged Adapter”. This will force the machine to get a locally-addressable IP address.

Under the “Shared Folders” tab in settings, click the “Add” button, set the folder path to some new folder on your Windows 7 system. Set the folder name to something simple like “nodejs”.

Under the “Storage” tab, click on the “IDE Controller”, then click the little CD button. Navigate to where you downloaded the Ubuntu Server ISO and select it.

Now boot your new Virtual Machine and follow the Ubuntu Server setup options. Install whatever your little heart desires, but I recommend an SSH server so you don’t have to install it later.

Once it’s installed, run the following commands to install VirtualBox Guest Additions:

sudo apt-get update sudo apt-get install build-essential linux-headers-$(uname -r) 
sudo apt-get install virtualbox-ose-guest-x11 

Next, create a “nodejs” folder in your home directory, and add the following to your rc.local file (use the command “sudo nano /etc/init.d/rc.local”):

mount -t vboxsf nodejs /home/[YOUR_USER_NAME_GOES_HERE]/nodejs

Restart and your Windows folder should be hooked up to the nodejs folder on your Ubuntu VM.

Step Three: Install Node.js

This part’s super-simple. Run the following commands:

sudo apt-get update sudo apt-get install git-core curl build-essential openssl libssl-dev 
git clone https://github.com/joyent/node.git && cd node ./configure make sudo make 
install node -v cd curl http://npmjs.org/install.sh | sudo sh

That’s it! You’re set up with Node.js and Node’s package manager, NPM. If you want to test it, go to your nodejs folder (in either Windows or Ubuntu, both should be working now) and make a file called “hello.js”, and put the following in it:

var sys = require('sys'); sys.puts('Hello!');

Then go to your Ubuntu system and run:

node hello.js 

Reference : http://blog.danhulton.com/2011/03/18/setting-up-a-virtual-machine-for-node-js-development-on-windows-7/

Diving into Node.js – A Long Polling Example

Node.js vs. The World

What is typical for most of the web servers is that they listen for requests and respond as quickly as possible on every one of them. In fact the speed of the response is one of the main targets of optimization for developers. Fast servers are what everyone needs. From web developers to website visitors!

In the field of the that battle different web servers have different “weapons” to gain time. While this is useful in most of the cases, when it comes to a chat-like applications and Node.jsapproaches, the response is not always immediately returned. As I described in my posts until now about Node.js, a simple web server may wait for an event to be emitted, and than return the response.

I wrote about how to write the very first server, but than I didn’t described how to make a “non-responding” server. By the term “non-responding” I mean a server that responds not immediately after it has received and parsed/executed the request.

 

Continue reading

Real time online activity monitor example with node.js and WebSocket

Here at New Bamboo, we specialise on Ruby On Rails web development. However, we started talking more and more about exciting things happening around HTML5 and javascript during morning standup (where we talk about all the cool things we are working/have discovered) , lunch time and our company hack day.

The latest hot topic is node.js. Some of us went to Full Frontal Javascript Conference and were very excited by the power and potential of node.js which Simon Willison (Django core team) introduced there. Simon describes node.js as “A toolkit for writing extremely high performance non-blocking event driven network servers in JavaScript”. I highly recommend you to read his blog post, “Node.js is genuinely exciting”

After reading Simon’s blog, watching Ryan Dahl’s talk video, and witnessing “A cambrian explosion of lightweight web frameworks based on top of Node” (again quotes from Simon) , I started thinking what would be an interesting app to write by myself.

Many people either created web framework, or chat apps, but I wanted to create something you wouldn’t normally do as web app, and this is what I came up with.

I’ve learned a lot while building this, and would like to share them step by step( or grab the code here if you are impatient to read my entire post.)

 

Continue reading

Why Everyone Is Talking About Node

On the 20th floor of a San Francisco skyscraper, a handful of developers are working on a new technology that’s changing the way real-time web apps are built and how they scale.

This technology, called Node.js, is being hailed as “the new Ruby on Rails” by some in the developer community. But it’s not a magic bullet, nor is it appropriate for all programming scenarios.

Joyent, an SF-based cloud software company, is sponsoring the growth and development of Node. It employs Node creator Ryan Dahl, hosts events and creates tools for the growing Node community. Recently, Mashablevisited Joyent to see demos from three startups that are using Node to build web and mobile apps for consumers.

In the process, we learned a lot about how and why Node works for the real-time web — and how Node is changing the way the developer community creates the Internet as we know it.


What Makes Node Different


Node had a watershed year in 2010, and it’s shaping up to be as popular as Ruby on Rails among developers. Since the framework was built for the commonly used JavaScript, the barriers to entry are remarkably low, and the reasons for choosing Node to build apps — especially low-latency, real-time apps — are increasingly compelling.

Then there’s the community. The Ruby community has been criticized for being exclusive and harsh. The Node community provides a welcome contrast and embodies the spirit of many other open-source communities. Again, the framework’s JavaScript roots means that it appeals to the less hacker-ish web designer who might be dabbling in web app development as much as it appeals to longtime, hardcore hackers who simply want a better way to build real-time apps.

We asked each of the three startups giving demos at Joyent: Why choose Node?

The common wisdom among many developers is that there is no single right language or framework that should and must be used for all web apps. But based on what we heard from these startups, Node is increasingly being seen as a “best solution” for a certain type of application.

According to Tom Hughes-Croucher, a recent Joyent hire who is writing the first O’Reilly book on Node, “Node has popularized event-driven programming.” With event-driven programming, Hughes-Croucher explains, “The actual amount of resources you use is much smaller, and you can get a lot more out of fewer servers.”

Node is all about making event-driven, low-latency, concurrent apps. Erlang, the language that powers Facebook’s chat server, uses the same model. Tornado, a concurrent server for Python that powers FriendFeed, was an attempt at this, too. But Node has one advantage over technologies like Erlang and Tornado: “None of that was too accessible,” says Hughes-Croucher. “Node takes a language people know very well — Javascript — and makes it available to do server programming, as well.”

In traditional languages and frameworks, the communication inside the app between the web server and the database is the most time-intensive part of the transaction. Node makes a much smaller footprint on your web server. It allocates web server resources on an as-needed basis, not pre-allocating a large chunk of resources for each user. For example, Apache might assign 8MB to a user, while Node assigns 8KB.

“The way that Node is more efficient on servers is by not allocating resources to things while it waits,” says Hughes-Croucher. “Say you have to talk to the database, and that’s going to take 50ms to respond. Instead of assigning all of the processing resources for that 50ms wait, it just uses a placeholder. When the database responds, then it allocates the resources needed to process. That means it’s totally possible to do a lot more requests at once, because you only allocate the server resources when you need to use them, not while you are waiting on databases.”


Node’s Explosive Growth


Unlike PHP or Ruby, Node has yet to appear as the technological face of a popular, mass-adopted web service like Twitter, WordPress or Facebook.

Rather, Node took off in the imaginations of programmers, organically becoming the quirky new tech that was on the tip of every tongue. First, devs asked if you’d heard of it; then, they started asking if you’d tried it or built anything with it.

As you can see from these GitHub reports, both the number of committers and the number of commits to Node core really took off, and there are no signs of Node’s growth slowing down in the dev community.

Click the image to see full-size charts, which we obtained from GitHub.

Commits and committers to Node on GitHub peaked in the fall of 2010, but developers’ conversations around Node have really just started to pick up steam since the beginning of 2011. Here’s a graph showing Twitter conversations around Node; you’ll notice a few spikes throughout the fall of 2010, and more consistent conversations occurring in 2011.

 

 

 

 

“If you look at Rails and Node on GitHub and compare the traffic,” says Hughes-Croucher, “Rails had 270,000 views over the past three months. Node has 325,000, and it’s only going up… it’s exploding.”

There have been “it” technologies in the past, and the current vogue is Ruby on Rails. So what makes devs think Node is the heir apparent?

“Node is going to become the next big thing for a few reasons,” said Hughes-Croucher. “Everyone wants to do way more powerful apps — things like Google Instant and Facebook. People are expected to support millions of users on a ton of devices in real time. These are the expectations people have of applications now. And if you did that with traditional frameworks, it would take forever, and the hardware would cost millions of dollars.

“The point of Node is that it’s really fast, it’s really easy to scale, and the Javascript aspect means it’s really easy to build.”


Node for Real-Time Voice


Igal Perelman is the VP of product for Voxer, an iOS app that aims to “make audio sexy again.” It functions a bit like a walkie-talkie, a bit like a group IM app and a bit like a social/location consumer app.

“There is a huge reason why kids love to use walkie-talkies. It’s fun and immediate,” said Perelman. “We took that basic use case and improved it a lot.”

The user pushes a button, starts talking, and the message is immediately sent to the app user on the other end. Both parties can listen and talk simultaneously. Chat requests come through as push notifications, allowing the users to choose whether or not to join the chat.

Users can also re-listen to messages in case they need clarification or missed part of a message, like rewinding a video. Another cool feature: The app supports group chats with unlimited numbers of participants.

Finally, it’s entirely free.

Voxer uses Node because the low latency allows for near-instant transfer of audio data. Says Perelman: “Node was very crucial to this, because the audio needed to be live. And Node allows us to maintain a large number of connections with very low latency. It was a quite easy decision.”

Voxer’s VP of technology, Matt Ranney, told us (via the app itself, of course), “This is our third iteration trying to do live voice. We first tried C++ for performance reasons, but it was too complicated, too hard to wrangle. Next, we chose Python. It was great, but unfortunately, the Python virtual machine is incredibly slow.

“So we’ve gone to the opposite extreme. In our third version, we’ve done it in Node, and we have the best of both worlds. We have the high-level language of Javascript and the high performance of the [Google’s V8 JavaScript engine’s] virtual machine.”


Node for Gaming


One of the hackathons we followed this year was Node Knockout, a 48-hour contest to see who in the world can build the best, most complete, most interesting Node apps on a very tight deadline.

The popularity winner for the 2010 contest was Scrabb.ly, a massively multiplayer online version of Scrabble. The game plays in real time, and the map of all the tiles is sprawling to gargantuan proportions.

The team built the game in two days and has since made a company out of the project.

On the flipside, “Rails Rumble [a similar, Ruby-on-Rails-flavored hackathon] has been around for four or five years, and only one company has come out of it,” says Fortnight Labs and Node Knockout co-founder Gerad Suyderhoud. “We had a company in our first year. Because Node is such a great technology for real-time web apps, you see a lot more games, so the results are a lot more fun.” The next Node Knockout is in August 2011.

Suyderhoud and co-founder Visnu Pitiyanuvath’s entry in the 2009 Rails Rumble was Lazeroids, but they discovered that building a real-time game in RoR was “too hard.”

When it comes to real-time games, says Suyderhoud, “There’s really not a lot that’s competing with [Node]. The other technologies just aren’t as good at doing real time. They’re backed in older frameworks. Node was designed from the ground up for real time and to be easy to use. Other technologies would take forever to do the same things.”

“You have this giant ecosystem of Javascript that’s perfect for real time,” he adds.

And as far as community is concerned, Suyderhoud says, “it’s amazing how inclusive it is. I’ve never seen such good support. For Node Knockout, it was some people’s first time using Node. We didn’t provide a lot of support, but we got really lucky. A lot of people who had no vested interest and were not participants stepped up and helped people solve their problems over the weekend, in chat channels and over Twitter. I was definitely not expecting that.”


Node for Collaboration


Mockingbird is more a tool for web designers than a true consumer app, and it allows devs and designers to rapidly create wireframes. The interesting thing about Mockingbird is that this Node.js app is already making serious money for its creators.

Basically, Mockingbird is a tool for collaborative drawing and real-time communication between designers and clients. It took the founders just months to take the app from concept to a working beta.

“We’ve been around since November of 2009 and launched our paid product November 2010,” says CTO and co-founder Saikat Chakrabarti. “We’re doing much better than we thought. We thought we’d be struggling entrepreneurs for a long time, but we’re very much in the green.”

The app currently has 60,000 users and hosts more than 100,000 projects.

Says Chakrabarti: “I tried to do the app in Twisted and Tornado at the time, and Node was by far the easiest… A lot of people and companies are very invested in this.”


Node for the Future


Time will prove whether Node is that next big thing, but one thing was agreed upon by all parties at this roundtable demo session: Node needs a Twitter, a Facebook or some other big, consumer-friendly, mass-adopted app to make the mainstream tech community take notice.

However, as soon as middle managers get wind of Node, it runs the risk of developing a bubble. An employment bubble would surely be followed by an employment crash, which would likely be perceived as the “downfall of Node.”

A misunderstanding of the technology is also a risk. Former Twitter engineer Alex Payne’s claim that Ruby was slow continues to haunt general conversations about Ruby to this day.

It’s not our intention to inflate anyone’s expectations of Node. And we’re certainly not advocating its use for all programming projects. However, we do think that Node is an interesting, accessible and efficient technology for real-time applications, and we’d love to see what comes of this framework.

We’ll especially be keeping an eye out for “the Twitter of Node” — both to critically examine how the framework performs under pressure and to keep an eye out for hype.

Stay tuned for Mashable‘s ongoing coverage of Node, including one-on-one interviews and code snippets to get you started.


Series Supported by Rackspace

 


rackspace

The Web Development Series is supported by Rackspace, the better way to do hosting. No more worrying about web hosting uptime. No more spending your time, energy and resources trying to stay on top of things like patching, updating, monitoring, backing up data and the like. Learn why.

Reference:http://mashable.com/2011/03/10/node-js/