Question

Suppose you are running a series of queries on a database (sqlite in this case) using Node.js. You know these queries may take a while to process and you want the client to update as the results come in. That is, you want to push the results to the client asynchronously, and then update the DOM.

I THINK I can cobble together a way of doing this using Faye (or some other comet system), JavaScript templating, and maybe Knockout.js. But is there already a framework or tool for doing this kind of thing?

Or is this just the wrong approach?

Was it helpful?

Solution

In previous projects I have used Socket.IO which makes pushing changes to clients easily. An example would be something like:

Server Code:

socket.emit('ranQuery', { info: 'Info From Server!' });

Client Code:

socket.on('ranQuery', function(data) {
    console.log(data.info); // would output "Info From Server!"
    // update the DOM with the new information
} 

There are plenty of working examples on their website. It can be installed like any other node package, npm install socket.io.


Update:

After opening and reading the links you posted I'd say your approach is correct. Though I've never heard of Faye it seems to do similar things as Socket.IO (sending objects from the server to the client), and KnockoutJS will handle the data binding (when you update models, it will update on the page as well). I'd say go for it and post questions here if you hit any snags.

The only reason I brought up Socket.IO was it's ease of use and documentation. I recently started a project using AngularJS + Socket.IO. If you're going to be making a lot of changes on the client side based on Frequent updates from the server, you may want to have a look at AngularJS as well and mess around with the examples on their website.

If you think that this will work for you, visit this blog post (http://briantford.com/blog/angular-socket-io.html) for a really great guide on getting AngularJS to work nicely with Socket.IO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top