NodeJS OpenFrameworks

Posted on April 4, 2012

2



Taking a user centred approach in implementation involves catering for the needs of a novice user, it was deemed necessary to design a graphical user interface for the NodeJS SocketIO server.

Currently to run the nodeJS server the user manually types in the command in the terminal to start or stop the server.

Messages from server are also logged in the terminal.

This method is prone to errors (in  this case error thrown because server could not connect to the database)

Resolved by starting mamp servers and restarting NodeJs server

This method is also susceptible to errors when typing in the commands, and it does not cater for a novice user that needs to learn the system, or  an occasional user that needs to remember  how to use the system.

The ofxNodejs bridge in OpenFrameworks runs a NodeJS instance from within OpenFrameworks.  OpenFrameworks will be used to create a graphical user interface for managing the server.

06-03-2012

The ofxNodejs is a nodejs-openFrameworks bridge created and recently updated by Satoru Higa at stuctor.jp connecting the event driven node I/O, built on the V8 javascript engine, and the openFrameworks c++ toolkit for creating coding.

The software is available on github – https://github.com/satoruhiga/ofxNodejs.  Download the folder and copy it to your openFrameworks addons directory and rename to ofxNodejs.  Navigate to ../…/openFrameworks-location/addons/ofxNodejs/simple-server-example and the example should run ( nodejs required). For mac users  Nodejs can be obtained by downloading from NodeJS website, or by building Libnode. (Also see post NodeJS _ https://agnetpro.wordpress.com/2012/03/09/nodejs/)

To add the library to the project folder  simply copy the example into your apps directory or add the ofxnodejs reference to project file (see readme file).  You can also add your node_modules by calling the ofxNodejs:: appendNodePath function.

08-03-2012

Currently, there are two ways to run nodejs code from within openFrameworks using ofxNodejs: 1. by copying nodejs code line by line into openFrameworks, or by ‘calling’ the local javascript (.js) file in openFrameworks.  Examples of both methods can be found at https://github.com/satoruhiga/ofxNodejs. Instructions for creating a nodejs module available – http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

It was decided to use the copy line by line method of code into openFrameworks.  The proposed reason being it would be simpler to use nodejs variables from within openFrameworks, which can then be displayed on the interface. The problem encountered at this stage was openFrameworks throwing an error “cannot find module ‘mysql’ “, that is, the ‘node-mysql’ module, which is being used to query database.

Screenshot showing error message in console:

There is a mysql bridge for openFrameworks – ofxMysql written by Elliot Woods. The addon was downloaded from github and added to project. When trying to connect to database, openFrameworks threw the errors-

“OF: OF_LOG_ERROR: mycontroller: Connection failed to database ‘products’ on host production ”

“OF: OF_LOG_ERROR: ofxMysql: Unknown Mysql server host ‘production’ (2)”

This was resolved by following instructions on the openFrameworks forum.  Instead of passing the parameter ‘localhost’  to the ofxMysql function, the parameter ‘127.0.0.1’ was passed , which enabled connection to the database.

The requirements of the Agricultural Network Project include returning results from a query to an SMS, therefore, the results from the c/c++ query had to be returned to nodejs.  Using the ofxMysql library also requires rewriting query functions that has already been written for nodejs – nodemsyql.  Hence, it would be better to try to get the node-mysql module working.

The node-mysql module was verified to work with ofxNodejs by Satoru Higa under the environment: MacOSX 10.7.3 + XAMPP + node(macports) + npm(macports), and the recommendation was to verify that nodejs was properly installed through npm and the node_modules PATH was set in the Global Environment.  It was discovered that there was a new version of nodejs available, and also, installing nodejs from the package available on the nodejs website did not install all the required libraries. (To view hidden files in Mac OSX please see – http://gigaom.com/apple/quick-tip-showhide-hidden-files/)

Screenshot showing version of nodejs installed and errors when nodejs is installed using the mac osx package file:

To resolve this problem macports (macports guide)was downloaded from – http://www.macports.org/install.php, and the package was installed on system. Nodejs was then installed through the terminal using the command : $ sudo port install nodejs  – and entering root/administrator password.  Npm was then installed using the command: $ sudo port install npm. Node-mysql was then installed using the command: $ npm install mysql.

Screenshot shows the results from reinstalling nodejs through macports, npm through macports, and mysql through npm:

Nodejs with node-mysql is now verified to work in openFrameworks using the ofxNodejs bridge under the environment: Mac osx 10.6.8 + MAMP + node(macports) + npm(macports).

11-04-2012

Problem encountered when trying to update/delete records with nodejs.

12-04-2012

Problem was due to incorrect statement.  A similar post was found in node-mysql google group mailing list – http://groups.google.com/group/nodejs/browse_thread/thread/155f6faf6361b292.  This was used as a guideline to rectify existing problem of updating records. System can now update existing records in database.

(Google search  node-mysql update record – http://www.google.co.uk/search?q=node-mysql+update+record&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a)

 

OpenFrameworks

C++ OpenFrameworks tutorial on creative computation – http://doc.gold.ac.uk/CreativeComputing/creativecomputation/?page_id=760

OpenFrameworks writing to screen – http://www.openframeworks.cc/documentation/graphics/ofTrueTypeFont.html

 

Advertisements