When you should use NodeJS for Mobile App BackEnd

JavaScript opens up new horizons. Nowadays miracles can be done with JS running on servers. So why not to use its advantages for your application BackEnd?

In this article we’ll take a look at NodeJS for mobile apps BackEnd from different points of view.

We’ll highlight the next topics:

  1. NodeJS and its history
  2. Why you should use NodeJS – pros and cons

You’ll get both upsides and downsides of this tool and, of course, practical advices on how to build a mobile app BackEnd with NodeJS!

NodeJS Logo

History behind

JavaScript language is becoming ubiquitous – it’s available in web browsers and on mobile platforms like Android and iOS.

Recently this practically all-purpose tool stepped into the reign of servers.

NodeJS is a fast-growing JavaScript runtime, based on Chrome V8 engine. Of course, running JavaScript on server-side has some upsides and some downsides.

We’d like to tell you why (and, what’s more important, when) you should use NodeJS for your BackEnd. For this purpose we’ll list its upsides and downsides.

Upsides

NodeJS uses non-blocking I/O model

It basically means that all the operations that require communication with external systems (e.g. databases, file systems, etc) are non-blocking.
In a nutshell, the app continues to run while it’s waiting for this communication to complete.

Here’s an example: Say, you need to read 2 big files and it takes 5 seconds to read each.
If you use NodeJS asynchronous file operations, reading both files might (and most likely will) take less than 10 seconds, as both operations will run in parallel.

In fact, If you read them from different sources, it might even take just a notch more than 5 seconds!

A lot of mobile app servers tend to use social media authentication, and this can often be represented as an I/O operation That’s a good reason to create your mobile app with NodeJS backend.

Apps that use social media usually store user data in databases, save user files, send out push notifications, etc. But basically all these are simple input/output operations!

And guess what? NodeJS works well with I/O operations out of the box!

Strong community

NodeJS has a lot of community-supported packages.

You wanna get authentication through social media? Then Passport.js (and packages like passport-instagram, passport-facebook, etc) will come in real handy!

Looking for some tools to work with databases? Try Sequelize, node-pg or squel.
Those solutions along with other database modules will definitely help you out!

Do you need to process images? Easyimage can simplify this task a lot.

All above is only a small fraction of all the packages available on npm and they are multiplying daily!

Mobile app BackEnd development can greatly benefit from all kinds of these modules!

Read also:

NodeJS is still “green”

NodeJS is quite a young environment and it’s developing even while you read this article!

It has an amazing opportunity not to make the same mistakes twice, based on the other environments experience. And you know what? Seems like NodeJS does a good job with that!

For many people using Javascript for the server might look like a bad idea, but it actually is not. NodeJS provides a way to omit bad solutions. and gives you more secure and cleaner solutions.

node

NodeJS is based on Chrome V8.

Chrome V8 is a JavaScript engine used in Google Chrome. NodeJS inherits lots of advantages from Chrome V8. It’s stable, fast and has a lot of cool features.

Thanks to the fast growing community, Chrome V8 is regularly updated, which constantly brings in new features and bug fixes.

NodeJS, of course, is not a silver bullet, but it’s a powerful tool worth taking a look at!
Nevertheless it has a bunch of downsides, making NodeJS not the best choice for some kinds of projects.

Downsides

NodeJS is single-threaded

Most of the applications nowadays are multi-threaded. This allows separating calculations and processing new requests from the outside into different threads, that are running in parallel.

NodeJS threading model vs traditional web server threading model
NodeJS, though, is single-threaded.This downside makes calculations inside NodeJS a blocking operation, which will become a bottleneck as the project grows.

Workarounds for the problem

However a lot of companies implement their mobile projects in a way that their server can often off-load nearly all the calculations either to a database or to external commands (like easyimage) or to a reverse proxy (for example, nginx) to do encryption, etc

In case you want to process push-notifications, you can start a second instance of your app to purely process all the push-notifications generated by the first instance and then make the instances communicate through a database.

Message queue schema
Here’s a good example of the aforementioned concept.

You can also run a bunch of NodeJS processes, and hide them behind a reverse proxy server – this often solves the problem of not using all of the multi-core system’s resources.

In addition, you can use cluster module, which has been recently added to help fixing this problem.

NodeJS uses JavaScript

As we already mentioned, JavaScript is notorious for all kinds of weird features. If it is used incorrectly server-side, it might result in a huge security risk! That’s why NodeJS requires developers to have some experience using JavaScript.

Nevertheless, NodeJS works its way around this problem. Guidelines like OWASP give detailed explanation of the vulnerabilities. We also recommend using so-called strict mode (as it eliminates lots of deprecated and vulnerable features).

Eval is evil

Finally, you should minimize the use of eval. Of course, it can be useful sometimes, but most of the time you can perfectly manage without it. This tip will be most useful for mobile app backend developers – it’s gonna help both boosting speed and increasing security of their code.

To sum it up, NodeJS can be used to write secure, fast and readable code, yet it does require some experience to do so.

0506.sdt-javascript

NodeJS requires a VPS

NodeJS is harder to deploy than a traditional server because of the fact that it needs a VPS.

There is no standardized way to deploy NodeJS. Plus, if you do not monitor it, it may simply crash from an exception (then it will stop replying at all).

Fortunately there already are some good solutions (Forever is a prominent example). However all of them require some setup.

Besides, it’s a bit hard to install on the server correctly, and it will run as root by default. That’s why you will have to lower the privileges programmatically from inside, and nvm can help with versioning and updating your installation.

To top it off, it’s practically impossible to find Node.JS hosting similar to traditional PHP hosting. There are services like Heroku, but it’s way closer to a VPS than to PHP hosting.

Conclusion

To decide whether using NodeJS is a good or a bad idea for you, try answering the following questions:

  1. Can you (your developer) offload most (better all) of the calculations to external services (database, other software, file system, etc)?
  2. Do your developers have enough experience?
  3. Do you have a system administrator to setup a VPS? If not, are you ready to go with Heroku or a similar service as your hosting?

If you can firmly answer “yes” to all three questions, then Node.JS is the right choice!

If you have any questions regarding Node.JS usage for your project feel free to contact us, and you’ll never regret it!

Comments

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *