Node.js async with events in order

Previously I wrote about Bookshelf, a wonderful and fairly powerful ORM for the Node world (and javascript in general). Considering how new Node really is to the public, that I can do most major things in MySQL with a few simple files and commands is just amazing.

But sometimes you want to do things in an order but the order isn’t know because it hasn’t arrived (new request or websocket message).

In comes a super simple tool called kue.
Here is a tutorial.

This allowed me to use a utility class along with a reversed controller that could accept messages, filter them out and then queue them to be saved to a database. This was required because some messages were updates to previous updates, with out build a super large container with its own comparison system, this allowed the queues to be processed in order, the promises to “.then” handle when the job was done and trigger that so that the next job could be processed.

https://github.com/learnboost/kue

Continue reading Node.js async with events in order

Working with Bookshelf.

Bookshelf is an awesome ORM.
http://bookshelfjs.org/
But the documentation is lacking in examples like non other.

I’m new to the Node world and almost all of these docs have the assumptions you know everything about Node outside of just this small function that the library provides.

First, since node is async, Bookshelf is also. Bookshelf uses promises (while others use callbacks).
This is great for a website but not great of a utility tool since you are dependent on the data being there before the next step.
So you have to wait for the ‘.then’ or the ‘fetched’ event (still figuring out how to use that…)

Example Code:

//js
var newUser = {
'name': 'Joe',
'email': 'joe@joe.com'
}

new self.models.User(newUser).save().then(function (model) {
console.log(model);
});

That looks nice and all, BUT, now you want to send an email, well you have to do that inside of the .save or have some kind of callback.
A way around this is emit another event.
http://www.wekeroad.com/2012/04/05/cleaning-up-deep-callback-nesting-with-nodes-eventemitter/

Dealing with .fetchAll and collection.fetch

Getting Started with Bookshelf.js

Continue reading Working with Bookshelf.

Node.js, PHP, NGINX and WebSockets (Socket.IO)

Don’t try using Express.IO. Just wasnt working.

Tech Used:
Centos 6.5 : http://www.centos.org/download/
Nginx : http://nginx.com/
MySQL : http://www.mysql.com/
Redis : http://redis.io/
PHP 5.5 : http://php.net/
Node.js : http://nodejs.org/
Socket.io : http://socket.io/docs/
Angular JS : https://angularjs.org/
Angular Socket IO : https://github.com/btford/angular-socket-io

Angular Socket IO Info:
http://www.html5rocks.com/en/tutorials/frameworks/angular-websockets/

Socket.IO works. But you have to understand what its doing. (I’ll explain further down)

Quick Info on Event Emitters:

Node.js Events and EventEmitter

Using Nginx to reverse proxy a secure apache site that is using socket.io/node.js/websockets
http://kenneththorman.blogspot.com/2013/07/using-nginx-to-reverse-proxy-secure.html

Loading a configurations in Node.js

Managing config variables inside a Node.js application


http://stackoverflow.com/questions/5869216/how-to-store-node-js-deployment-settings-configuration-files

Continue reading Node.js, PHP, NGINX and WebSockets (Socket.IO)