Using Redis as Node.js Express Session Storage on Heroku

The default way to use sessions with Node.js and Express is using the bundled memory store. However, if you want to deploy your app to Heroku and use multiple dynos it won’t work because the memory store doesn’t work across dynos.

The solution is to use a database for session storage and it seems like Redis or MongoDB are the most common used together with Node.js and Express. Here a quick walk through to setup Redis for this purpose. The example is using CoffeeScript.

First you want to install Redis on your machine to test against localhost. Download the source and then:

Install the Redis add-on on Heroku: $ heroku addons:add redistogo
You can check your config to make sure process.env.REDISTOGO_URL is available: $ heroku config

Now the implementation in your main app.coffee file:

Make sure to add the new dependencies to your package.json:
“connect-redis”: “1.3.0″, “redis”: “0.7.1″

That’s pretty much it. Now just scale your dynos: $ heroku ps:scale web=2

Thanks to Jerod Santo for his blog post which got me started.
Also read the docs on Express Session Handling.

New Machine Setup

Recently I’ve got a new work Mac Book Pro.
Here a a summary of my initial setup:

Preferences

  • Tap to click
  • Natural scrolling

Software

  • Chrome
  • Firefox
  • 1Password
  • Dropbox
  • Evernote
  • Sublime Text 2 (with packages for CoffeeScript and Stylus)
  • iTerm 2
  • GIT
  • node.js
  • Heroku toolbelt
  • Mongo DB
  • Spotify
  • Skype
  • Adium
  • Kindle
  • Twitter
  • XCode

Global NPM packages

Command line

  • Custom bash prompt
  • Heroku SSH
  • Shortcut for Sublime (sudo ln -s “/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl” /usr/local/bin/subl)
  • Show hidden files (defaults write com.apple.finder AppleShowAllFiles TRUE & killall Finder)

Node.js server-side compile Hogan.js templates

If you are using Node.js with Hogan.js you want to compile your templates on the server.
With Node.js you can create a route which looks like this:

Include it like: <script src=’templates’ type=’text/javascript’></script>
Templates are in the T namespace and can be renderered like this:

CoffeeScript with Sublime Text

Recently I’ve played with node.js and the Play! framework and I came across CoffeeScript.
CoffeeScript is a language which compiles to JavaScript. The advantage over using JavaScript is basically having a more compact language which looks a lot like Ruby. Sometimes one very readable line of CoffeeScript translates into 7-10 complex lines of JavaScript.

My dear friend Alec recommended Sublime Text, a relatively new lightweight editor similar to TextMate. Starting to learn CoffeeScript I decided to give Sublime a shot and I liked it a lot. Now I also use Sublime to do the Java coding for Play! framework code. Usually you want a richer IDE like Eclipse or IntelliJ for Java but since Play! compiles the code on the fly and comes with perfect compile errors in the browser, a lightweight editor like Sublime works just fine.

Here a little tutorial how to setup Sublime to start coding CoffeeScript:

  1. Install CoffeeScript (You need to have node.js and the node package manager installed):
    npm install -g coffee-script
  2. Download and install Sublime Text Editor
  3. Save the CoffeeScript.tmLanguage here: ~/Library/Application Support/Sublime Text 2/Packages/CoffeeScript/
  4. Create the CoffeeScript.sublime-build here: ~/Library/Application Support/Sublime Text 2/Packages/Users with the following content:
    {"cmd": ["coffee", "$file"],
    "selector" : "source.coffee",
    "path" : "/usr/bin"}
  5. Now open Sublime and create a helloWorld.coffee:
    console.log "Hello world"
  6. CMD-B to execute the script and you should see the output:
    Hello world
    [Finished]

Another helpful thing is to configure Sublime to be used from the command line. Open a terminal and create a symbolic link:

sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/bin/subl

Now you can change to the directory of you project and with “subl .” Sublime opens the current directory as a project.

To get an overview how CoffeeScript compares to JavaScript check out the docs. Further I’ve read CoffeeScript: Accelerated JavaScript Development which gives a good overview of the language.

Update: Great link explaining some Sublime features/shortcuts 9 reasons you must install Sublime Text 2. Code like a man, man.

Detect Spark List Control Scroll To End

If you want to implement lazy loading (when the user scrolls to the end of the list it triggers to fetch more items) for a spark List control you have to use a workaround right now.

Usually you would add an event listener on the scrollers verticalScrollBar but due to a bug it does not fire events if the user scroll with the mouse wheel or uses the arrow buttons. The bug shows a workaround by listening to the PropertyChangeEvent of the scrollers viewport.

Here a little example app:

The bug has been fixed for Flex 4.5. If there is a better workaround for Flex 4.1 please let me know.

Compile-time FXG

FXG was introduced with Flex 4 and offers a great way to skin and style your application without relying on bitmaps like PNGs.

“FXG is a declarative XML syntax for defining vector graphics in applications built with Flex.”

When you declare FXG within your MXML it is computed at runtime but there is also a way to use compile time FXG. There is up to 10x improvement in memory consumption and start-up time when converting complex MXML Graphics (with lots of Paths) into FXG.

Deepa explains it like this:

“This is an optimization scheme which takes advantage of the fact that a lot of FXG content is simply static assets that do not need to be updatd or re-rendered at runtime. In cases like that, you could imagine that instead of creating ActionScript objects to represent those graphic elements, the Flex compiler could instead create SWF graphic primitive tags to draw the static FXG asset”

So there is some benefit in using compile time FXG and in many cases you can just copy and paste the FXG to an .fxg file with some minor syntactical changes. The downside of compile time FXG is that you have to specify fix dimensions. This means you cannot use percentage values for the width and height.

Let’s look at an example which uses FXG to define the background of a Group.
Comparison:

Runtime FXG

Compile-time FXG

You see that you can instantiate the Background.fxg like any other class.
The differences between compile and runtime are fairly simple:

  • Add a Graphic tag
  • Replace spark namespace with default namespace
  • Change numeric values 0x to #
  • Define fix width and height

Another difference is that you have to apply a scale-9-grid for the background in case of rounded corners and filters. This would like like this:

These are still simple examples and it can get way more complex and you will run into limitations. Compile time FXG was developed by Peter Farland from the Flex side and he gave some inside about the limitations of compile time FXG in combination with scale-9-grid:

  • An item with a scaling grid cannot have child <Group> elements (because ata lower level a scaling grid cannot be applied to a DefineSprite that has a child DefineSprite(s)… this also means that you can’t use a scaling grid with children that are ultimately DefineSprite based including <RichText>,or a custom Library Definition, and I think if alpha masks, luminosity masks, or pixel-bender based blend modes (such as colordodge, colorburn,exclusion, softlight, hue, saturation, color, luminosity) are applied).
  • A scaling grid must be a valid rectangle – i.e. the resulting rect must have a non-zero width and height and the left extent must be to the left of the right extend, and the top extent must be above the bottom extent.
  • The scaling grid¹s center rectangle must be wholly within the bounds ofthe item(s) being scaled.
  • Rotated items cannot be scaled with a scaling grid (I think? though notethat restriction doesn’t apply to rotated gradient fills applied to shapesbeing scaled, for instance).
  • There are several bugs when applying a scaling grid to a collection of child shapes – adding a transparent Rect underneath that spans the bounds of all of the child shapes can help.
  • When manually calculating the dimensions of a scaling grid you need to consider the total bounds of the item(s) being scaled. This must take into account the stroke width, miter and joint types. Watch out for fractionalpixels of strokes that apply once an item is scaled.

Conclusion
Try to use compile-time FXG when possible. The performance of your application will improve and you have a better separation of design and code which is one step further for the designer/developer workflow.
For further reference check the FXG 2.0 Specification

.