Wynn Netherland changelog.com/posts

nodestream: Templating for realtime apps in Node.js

Technologies like Websockets lead us to rethink how we construct our web applications. Model-View-Controller (MVC)-based frameworks have encouraged us to chunk up markup into templates that are served up in response to controller actions. Different approaches for updating application views are emerging as browser clients take on more and more responsibilities.

For Node.js applications, LearnBoost has released Nodestream which adds realtime template updating to your application. Take the following Express application snippet and Jade template:

// app.js
app.get('/', function(req, res){
        res.render('items.jade', {locals: { items: items }})

app.post('/save/item', function(req, res){
    database.saveItem(req.body, function(){

// items.jade
- each item in items
        p This is an item titled #{item.title}

Nodestream allows your templates to get updated in realtime with Socket.io using Websockets and JavaScript with only a couple of changes.


First we need to notify Nodestream of an update, emitting a newitem event when we save an item to the database:

    nodestream.emit('newitem', item)

… and then hook into that even in our template:

:realtime(append: 'newitem', local: 'items', obj: 'item')
        p This is an item titled #{item.title}

Now our views are updated after new items are add, even in multiple tabs! Awesome.

Be sure and check the README for full installation instructions and other usage guidelines.

[Source on GitHub]


Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00