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 }})
})'/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 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]

News Films

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

GopherCon 2016 Conference Recap

0:00 / 0:00