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){
    database.getItems(function(items){
        res.render('items.jade', {locals: { items: items }})
    })
})

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


// items.jade
- each item in items
    div.item
        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.

Usage

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

database.saveItem(item){
    ...
    nodestream.emit('newitem', item)
}

… and then hook into that even in our template:

:realtime(append: 'newitem', local: 'items', obj: 'item')
    div.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]

News Films

Our little film studio focuses on telling developer-centric stories that need to be seen.

Beyond Code: Season 3 / GopherCon 2015

 
0:00 / 0:00