Monday, October 7, 2013

Node.Js Async Processing Example


  • The BeagleBone Black supports a number of development environments native to other linux platforms.  My original thought was to use Python but a closer look at Node.Js and Javascript convinced me to go in that direction.
  • 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.
  • Using Javascript on both the server and client will make development more consistent and will allow access to a realm of addons for graphing and other functions. 
  • The “event-driven, non-blocking I/O model” mentioned of Node.Js does pose a bit of a challenge in terms of the structure of programs written for the node environment.
  • The below diagram presents an example of how this works.
  • Code written in this manner allows the node engine to focus on processing tasks that need attention rather than on those waiting for slower operations completing.
  • It can lead to a plate of call back spaghetti  ... and has not been the easiest environment in which I have ever worked!
  1. Function that sends the specified file of html to the client.
  2. Asynchronous read of the file initiated here with two arguments – the file name to read and the function to call when the read has been completed.
  3. Inline definition of the function mentioned above – the call back function named as such because it is called back when the read finishes.
  4. (and 5.) In this example the message “sendForm ended” will appear on the console BEFORE the message “Form xxx Sent"
Sample code for my demo application can be found on GitHub.

No comments:

Post a Comment