Store data with CloudBoost's CloudObjects
CloudBoost is a Database Service with storage, search and real-time capabilities. Think of CloudBoost as Parse + Firebase + Algolia all combined into one. CloudBoost has SDK's in multiple platforms which helps you build web, mobile and IoT apps without dealing with the backend and database infrastructure.
Read more...Turbo Charge your NodeJS app with Cache
Caching is great for your apps because it helps you to access data much faster when compared to the database. On the downside, querying is limited and it is very expensive (money-wise) because all the data is on the memory (which is expensive) instead of being on a disk. It is recommended that you use cache only for frequently accessed data.
Read more...Node.js For Beginners. Deploy Your Blog to Heroku
Error pages are not what typically appear on your screen when you're surfing the web, but when it happens it's so annoying! To see how servers work from within, we will build a simple web server by ourselves. We will use Node.js as a server part technology for that task. Then we'll use Heroku cloud application platform to turn this local server into a world wide server.
Read more...Solving Coding Challenges with Streams
My first experience using Node.js for a programming challenge was agonizing. I devised a viable solution, but I couldn’t figure out an effective way to parse the input. The format was painfully simple: text piped via stdin. No problem, right? I burned over half my time on what should have been a minor detail, and I ended up with a fragile, zip-tie and duct tape hack that still makes me shudder.
The experience inspired me to find an idiomatic approach to programming challenges. After working through more problems, I arrived at a pattern I hope others will find useful.
Read more...Generators vs Fibers
Both ES6 generators and node-fibers can be used to block a coroutine while waiting on some I/O without blocking the entire process.
This means that you can wait for an I/O result within your code but still have the performance benefits of the single-threaded, non-blocking I/O model you all know and love.
Both can do this for arbitrarily deep call stacks. The main difference between the capabilities of the two is how explicit the syntax is. It's the classic battle between wanting to be safe while wanting to be flexible.
Read more...
Simplifying Chores with Grunt
Often in various stages of development we perform repetitive tasks e.g minification, compilation, unit testing, linting and we would definitely want to automate them. But each of these task require different configurations or installations. What if there is a system where in we can easily configure and run these tasks from a single place. Also it would be really nice if it gels well with Node.js system. Meet Grunt a task runner for simplifying chores. Apart from being simple Grunt ecosystem is huge and it's growing every day with literally hundreds of plugins to choose from.
Note that I will be using grunt task/plugin interchangeably which means the same.
Read more...Daddy, what's a stream?
At dinner tonight, I was discussing that I've been thinking about streams and how to simplify them in JavaScript. My 7-year-old perked up and asked me:
Daddy, what's a stream?
I explained that a stream is like a line of water along the ground. If you dig a trench on a hill and fill up the high end using a water hose the water will run down the hill along the trench. This is one of his favorite hobbies in the back yard, so he understood completely. I explained that when new water is added to the top, it eventually makes its way to the bottom of the stream. The end where water comes from is called the upstream and the other end is the downstream. Gravity pulls water down.
Read more...Content Syndication with Node.js
Web syndication is a must for any Website wishing to share some entries easily to other systems. Better known under their format name like RSS) or Atom), they can be quite time consuming to generate without a module handling all their formating. Thanks to the power of Node's package manager NPM, you can generate yours in no time.
Read more...Session-based Authorization with Socket.IO
Finding a decent article about session based authorization in socket.io is more difficult than one expected. This article will show how you can take advantage of Express session middleware and socket.io authorization middleware to create a very simple authorization mechanism when establishing new socket.io connections.
Read more...Make Your Tests Deterministic
Non-deterministic issues like race conditions or thread deadlocks are very difficult to test, because they are by nature hard to reproduce. Fortunately, in the JavaScript world, we only have a single thread, so we are safe, right? Well, not really because...
Execution order of callbacks can cause the same race condition issues that plague multi-threaded environments.
Read more...Sending e-mails with Node and NodeMailer
Sending e-mails with NodeJS is almost a breeze. Almost. First, you have to plug-in the NodeMailer module than set up a transport type, load the templates, add attachments and finally send...
Read more...Node.js and MongoDB - Getting started with MongoJS
It won't be an exaggeration if one claims that in the past few months Node.js and MongoDB have literally taken the software and web industries by storm.
Not just bleeding-edge startups but even medium and large enterprises are leveraging these two technologies to deliver a better experience to their users by build more capable, performant and scalable apps.
So what is Node.js?
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
..and what is MongoDB?
MongoDB is a scalable, high-performance, open source NoSQL database.
This post will cover the basics and get you started with your Node.js + MongoDB app. Make sure you have Node.js installed and MongoDB setup on your developer machine.
Let's verify your Node.js installation and start the MongoDB server:
$ node -v
$ mongod
Read more...Really simple file uploads with Express
Few days ago I was working on a fairly typical web application and I faced the challenge of implementing a fairly typical web application feature - file uploads. It was the first time I was implementing file uploads with Node (and Express) and I did what anyone else would do - I googled it.
Unfortunately all the articles / posts out there are either outdated, too complex or plain wrong. So I did the next most obvious thing - post a question on the mailing list. As always Mr. Holowaychuk was incredibly quick to respond. His answer lead me to do what I should have done in the first place - read the docs.
Read more...Understanding process.nextTick()
I have seen quite a few people being confused about process.nextTick()
. Let's take a look at what process.nextTick()
does, and when to use it.
As you might already know, every Node application runs on a single thread. What this means is that apart from I/O - at any time, only one task/event is processed by Node's event loop. You can imagine this event loop to be a queue of callbacks that are processed by Node on every tick of the event loop. So, even if you are running Node on a multi-core machine, you will not get any parallelism in terms of actual processing - all events will be processed only one at a time. This is why Node is a great fit for I/O bound tasks, and definitely not for CPU intensive tasks. For every I/O bound task, you can simply define a callback that will get added to the event queue. The callback will fire when the I/O operation is done, and in the mean time, the application can continue to process other I/O bound requests.
Given this model, what process.nextTick()
actually does is defer the execution of an action till the next pass around the event loop. Let's take a simple example. If we had a function foo()
which we wanted to invoke in the next tick, this is how we do it:
function foo() {
console.error('foo');
}
process.nextTick(foo);
console.error('bar');
If you ran the above snippet, you will notice that bar
will be printed in your console before foo
, as we have delayed the invokation of foo()
till the next tick of the event loop:
bar
foo
In fact, you can get the same result by using setTimeout()
this way:
setTimeout(foo, 0);
console.log('bar');
However, process.nextTick()
is not just a simple alias to setTimeout(fn, 0)
- it's far more efficient.
More precisely, process.nextTick()
defers the function until a completely new stack. You can call as many functions as you want in the current stack. The function that called nextTick has to return, as well as its parent, all the way up to the root of the stack. Then when the event loop is looking for a new event to execute, your nextTick
'ed function will be there in the event queue and execute on a whole new stack.
Let's see where we can use process.nextTick()
: