Friday, 27 January 2017

Node more popular than Rails

1 Introduction

The popularity of JavaScript applications has been skyrocketing in the last few years, with Node.js definitely facilitating this growth. If we look at modulecounts.com we will see that there are more Node packages in the wild than in the Ruby world. In addition, Node packages are growing faster than Ruby, Python, and Java combined.

Data from modulecounts.com (collected by scraping the relevant websites once a day) In this article we are going to take a look at the most important aspects of Node so you can get on track with it and start building applications right away.

Node markets itself as an asynchronous, event-driven framework built on top of Chrome's JavaScript engine and designed for creating scalable network applications. It's basically JavaScript plus a bunch of C/C++ under the hood for things like interacting with the filesystem, starting up HTTP or TCP servers and so on.
Node is single-threaded and uses a concurrency model based on an event loop. It is non-blocking, so it doesn't make the program wait, but instead it registers a callback and lets the program continue. This means it can handle concurrent operations without multiple threads of execution, so it can scale pretty well.
In a sequential language such as PHP, in order to get the HTML content of a page you would do the following:
$response = file_get_contents("http://example.com");
print_r($response);
In Node, you register some callbacks instead:
var http = require('http');

http.request({ hostname: 'example.com' }, function(res) {
  res.setEncoding('utf8');
  res.on('data', function(chunk) {
    console.log(chunk);
  });
}).end();
There are two big differences between the two implementations:
  • Node allows you to perform other tasks while waiting to be notified when the response is available.
  • The Node application is not buffering data into memory, but instead it's outputing it chunk-by-chunk.
While other event loop systems exist (such as the EventMachine library in Ruby or Twisted in Python), there is a significant difference between them and Node.
In Node, all of the libraries have been designed from the ground up to be non-blocking, but the same cannot be said for the others.

1.2 npm, the official Node package manager

Node owes a big part of its success to npm, the package manager that comes bundled with it. There are a lot of great things about npm:
  • It installs application dependencies locally, not globally.
  • It handles multiple versions of the same module at the same time.
  • You can specify tarballs or git repositories as dependencies.
  • It's really easy to publish your own module to the npm registry.
  • It's useful for creating CLI utilities that others can install (with npm) and use right away.

2 Installing Node.js and NPM

There are native installers for Node on Windows and OS X, as well as the possibility of installing it via a package manager. However sometimes you will want to test your code with different Node versions, and that's where NVM (Node version manager) comes in.

With NVM you can have multiple versions of Node installed on your system and switch between them easily. In the next few lines we are going to see how to install NVM on an Ubuntu system.

First, we have to make sure our system has a C++ compiler:

$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev
 
After that we can copy-paste the one-line installer for NVM into the terminal:

$ curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash

At this moment NVM should be properly installed, so we will logout and login to verify that:
 
$ nvm

If there's no error when typing in the nvm command, that means that everything's alright. Now we can move on to actually installing Node and npm.
 
$ nvm install v0.10.31

The output should look like this:
 
$ nvm install v0.10.31

################################################################## 100.0%

Now using node v0.10.31

Both node and npm should be available in the terminal now:
 
$ node -v && npm -v
v0.10.31
1.4.23

There is one last thing we need to do so we can always use this version of Node when we login the next time: making this version the default one.
 
$ nvm alias default 0.10.31

We can install other Node versions just like we did before and switch between them with the nvm use command:
 
$ nvm install v0.8.10
$ nvm use v0.8.10

In case you are not sure what versions you have installed on your system just type nvm list. That will show you the full list of versions and also the current and default versions, such as the following:

$ nvm list
v0.6.3 v0.6.12 v0.6.14 v0.6.19 v0.7.7 v0.7.8 v0.7.9 v0.8.6 v0.8.11 v0.10.3 v0.10.12 v0.10.15 v0.10.21 v0.10.24 v0.11.9 current: v0.10.24 default -> v0.10.24


Friday, 16 September 2016

Node.js vs PHP



Node.js vs PHP, is a lot like Batman vs Superman: both have different superpowers, both are entangled in a never-ending battle and most important of all, their fan base comprises of nerds like us!
Server-side developers have a bewildering choice of long-standing heavy-weights such as Java, C, and Perl to newer, web-focused languages such as Ruby, Clojure and Go. It rarely matters what you choose, presuming your application works.
But how do noobs, the new ones to web development make an informed decsion? I hope not to start a holy war, but I’m pitting two development disciplines against each other:
Restricting Our Smackdown to PHP vs. node.js
Round 1: Getting Started
How quickly can you build a “Hello World” web page? In PHP:
<?php
echo ' Hello World!';
?>
The code can be placed in any file which is interpreted by the PHP engine — typically, one with a .php extension. Enter the URL which maps to that file in your browser and you’re done.
Now, let’s create our web page in hello.js:
var http = require('http')
http.createServer(function (req, res){
res.http.writehead(200, {'ContentType': 'text/plain'});
res.end('Hello World!'):
}).lsiten(3000. '127.0.0.1');
You need to start the app from the terminal with node hello.js before you can visithttp://127.0.0.1:3000/ in your browser. We’ve created a small web server in five lines of code and, amazing though that is, even those with strong client-side JavaScript experience would struggle to understand it.
PHP is conceptually simpler and wins this round. Those who know a few PHP statements can write something useful. It has more software dependencies, but PHP concepts are less daunting to new developers.
Round 2: Usage/Compatibility
Where can both the technologies be employed. Which platforms are supported? In their career, Web developers often create applications which aren’t limited for just usage on the web, e.g. migration tools, database conversion scripts, etc.
PHP is a server-side development technology at heart. It’s great at that job but is rarely stretched beyond it.
JavaScript would have been considered more restrictive a few years back, its main place was in the browser.
Enters Node.js, It has changed that perception and there has been an explosion of JavaScript projects. You can use JavaScript everywhere — in the browser, on the server, terminal, desktop and even embedded systems. Node.js has made JavaScript ubiquitous.Makes Node.js a safer bet because of its wide adoption in short span of time.
Round 3: Seeking Assistance
When it comes to web development you won't get far without seeking some help. You will need to refer to the official documentation and other resources on the web such as forums and Stack Overflow. PHP has a great advantage when it comes to this, manual and twenty years’ worth of Q&As. In fact you would be shocked to know “how a tweet changed PHP as a programming language”. It’s a great Web development read. Almost makes a good movie script for nerds.
Node.js has good documentation but is less mature and there is less assistance available. JavaScript has been around as long as PHP, but the majority of assistance relates to in-browser development. That rarely helps. With time surely the assistance will grow and diversify but for now PHP is ahead by miles.
PHP wins this round easily.
Round 4: Friendly Syntax
Unlike some languages, PHP doesn’t force you to work in a specific way and grows with you. You can start with a few multi-line programs, add functions, progress to simple PHP4-like objects and eventually code beautiful object-oriented MVC PHP5+ applications. Your code may be cluttered at the start with, but it’ll work and evolve with your understanding.
JavaScript remains the world’s most misunderstood language — but, once the concepts click, it makes other languages seem cumbersome And since you mentioned you have already learnt Javascript you might get friendly with node.js rather easily.
Node.js wins this round.
Round 5: Development Tools
Both technologies have a good range of editors, IDEs, debuggers and other tools. There’s one tool which gives Node.js an edge: npm — the Node Package Manager. npm allows you to install and manage dependencies, set configuration variables, define scripts and more.
PHP developers will probably want/need to install Node.js at some point. The reverse isn’t true.
This only makes the case of learning node.js over PHP stronger.
Round 6: Integration
PHP is unbeatable in this area. It’s been around for many years, it is well established and has strong connections. It allow direct communication with a host of popular APIs.
Node.js is catching up fast, but you may struggle to find mature, secure integration components for less-popular technologies.
Round 7: Hosting and Deployment
How easy is deploying your shiny new app to a live web server? It’s another clear win for PHP. According to a survey done by http://W3tech.com, PHP is used by 82.3% of all the websites. Contact a random selection of web hosting companies and you’ll discover the majority offer PHP support.
Node.js is a different beast and server-side apps run continuously. It requires a specialist server environment. That’s something out of reach of some hosts, especially on shared hosting where you could bring down the whole system.
Node.js hosting will become simpler, but I doubt it’ll ever match the ease of FTP’ing a few PHP files.
Round 8: Performance
Node.js is 10X faster than PHP:
Node.js runs on servers using Google’s V8 engine. V8 compiles JavaScript to native machine code, thereby compiling and executing JavaScript code at a lightning speed. Moreover, Node.js uses event loop to process I/O operations asynchronously. It means that multiple I/O operations do not block each other.
On the other hand, PHP is synchronous, i.e. if one I/O operation is getting processed, other I/O operations will have to wait till it gets processed. Although this problem can be solved using ReactPHP, but Node.js is still better than it by a mile!
Round 9: The Future
PHP while it might be a safe bet and support looks assured for another twenty years. But
The ascent of Node.js has been rapid. It offers a modern development approach, uses the same syntax as client-side development and supports revolutionary HTML5 features such as web sockets and server-sent events. There has been some confusion regarding forks of the language but usage continues to grow at an exponential rate.
Winner: Node.js
Node.js promises cost cutting software lifecycles that can be scaled without spending fortunes.
If you are building an application that requires working with APIs or depends on several underlying parallel services – Node.js is a powerful horse that you can bet on with closed eyes