node.jsmongodbexpresshandlebars.jslivereload

Handlebars Hot Reload


I'm building a betting web application through Node, Handlebars, Express and Mongo. One of my key requirements is to have live updates of betting odds as they change in the database. Currently, the data is rendered on a Handlebars template via Mongoose queries. Here is a sample where 10 'To Win' markets are rendered on the homepage, as well as carrying over the user's session details:

app.get('/', function(req, res, next){
        Market.find({"marketname" : 'To Win'}).limit(10)
            .then(function(doc){
                res.render('index', {items: doc, user: req.user});
        });
});

My problem is that odds can fluctuate regularly and I have no way to trigger a re-render with the updated data without either refreshing the page or navigating to another link and back to the given page. If any more information is required I'll edit the post appropriately.


Solution

  • You can use web sockets to handle the real time aspect of your app. There are a number of good libraries and articles on the web that can help you with implementation.

    If you're flexible about which database to use, you could also look into a push based database such as RethinkDB.