3

I see a lot of tutorials who use node.js with Raspberry pi (and even with the Raspberry Pi and Arduino working together). I also see a lot of examples in Python (Python is already installed in Raspbian images, node.js isn't).

So I would like to know the pros and cons of using node.js or Python's Twisted for communicating with the internet on the Raspberry Pi.

1
  • If you ask what car is the best, how many answers do you think you can get?
    – avra
    Apr 3, 2014 at 7:58

2 Answers 2

4

The only real answer to this question is "it depends". I'll do my best to outline what it depends on, and which platform is suited to which cases. But first, a couple of clarifications.

Node.js describes itself as "a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications" [1]. JavaScript is also the programming language used by web browsers to make pages interactive, so if you want client side interactivity you'll need to learn JavaScript - if you don't know Python, it may be worth just learning one language.

I'd suggest the key thing you should base your decision on is what you're using it for. If you want to host something on the Pi and access it on a web page, I'd suggest you use Node.js. It has better performance, a nicer way of managing packages with npm and several battle-tested web frameworks such as express.

However - if you're accessing the internet as a client - perhaps scraping web pages, sending tweets or up/downloading files, Python is probably better. This is a gut feeling, and purely my opinion, but Python feels nicer for such a use case and has a large number of libraries such as urllib2 or requests (the latter of which is available through pip, a python package manager).

Python also has the advantage of being pre-installed, as you mentioned, and is the programming language the Raspberry Pi is designed for use with. It also has exceptional support for GPIO interaction, so if you're planning on exposing control of GPIO pins (such as a web page which lets you control an LED), I'd recommend you go with Python.

There are several Python web frameworks available, some of the more well known ones are Flask, web2py and Django, amongst others.

Again, this is largely opinion, though I've used both Python and JavaScript (with and without Node.js) extensively.

1
  • Great answer. I only know the very basics of Python but I've used Node.js for a few things. If you do decide to use Node for screen scraping I'd really recommend cheerio and request. I say +1 for using Python if you want to control GPIO or other Pi-related stuff as there are more examples. Dec 12, 2013 at 16:15
2

Explicitly addressing Twisted python vs Node.js (and vs normal python)

Unlike normal python, the Twisted framework turns python into a kernal controlled event framework, which is also effectively what Node.js is about as well.

Both node and twisted require that your write in (or at least are aware) of event style programming, which is a bit alien to normal python programers (though familiar to embedded programers)

While I've seen wild claims, the performance of the Twisted and Node are likely to be similar for most projects because they both are driven by the kernal epoll() system. (I.E. much of what your doing is designing your logic around the kernal event structure, the efficiency in either platform really comes down to how well you designed your logic)

From a codebase/community standpoint, the most common thing I hear is:

  • Twisted has more well tested odd protocol support then Node, but its community isn't really growing (though nor is it really getting smaller - and I suspect the same could be said for python overall)
  • Node has a much newer, less battle-proven codebase with less odd-ball protocol support, but it's community is on fire and it seems to be where the new dev action is going.

Given all that, I would say in Twisted vs Node you would be best choosing by some other soft need of your project, they are too similar for there to be a clear winner.

I personally chose Twisted for my RaspPi dev because many people around me had been telling me how it was crazy capable but seemed completely unable to explain what it was at its core, so since I was working on a hobby project, I picked it to solve the mystery and to get to better-know python (little did I know that Twisted is so different from pep8 python (because Twisted pre-dates pep8!) that most classic python programers would still assume I'm not a python programer because I write in the Twisted style)

You can dive into quite a rabbit hole on this topic, but there is lots of good info out there, particularly on stack overflow

BTW if you do dive into twisted, strongly recommend krondo Twisted Introduction, particular if you have a epoll()/select()/event-programming background. Its very comprehensive (which also means its really long) but by the end you'll really understand the core of Twisted (much better then most of the people that claim to know Twisted)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.