2

I am using working on web app which had a DIV in which latest update about a topic is shown which is coming from some other website. So what i want is to update the DIV as soon as the data is really updated in back end database.

On the back-end I am using cron to fetch & store data in database.

A simple way of doing this is to use setInterval() function and keep on fetching the data at a particular time interval but I think this would just cause unnecessary load on the server and just waste CPU cycles.

So what's the best way to implement this? can/should i use nodeJs for this?

-Thanks

P.S. I just want something like facebook uses to upddate it's news feed in the middle of page.

and i have no experience with nodeJs yet so honestly i don't even know that nodeJs is good for this or not.

Ted
  • 1,641
  • 7
  • 30
  • 52
Peeyush
  • 4,728
  • 16
  • 64
  • 92
  • Recursive `setTimeout()`. Or `setInterval()`, which will be clamped when not the current tab/window, which sounds useful in your context. – alex Jan 26 '12 at 02:43

3 Answers3

2

If the problem you're trying to solve is how to efficiently update your browser display without repeatedly polling the backend server, then you can use "long polling" in which you make a web server request with a long timeout and the request only returns when there is actually some new data or it eventually times out. You can read more by searching Google or SO for "javascript long polling". Here are a few of those references:

How do I implement basic "Long Polling"?

How to implement Server push / long polling / comet using PHP and Javascript

http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

In modern browsers, you could also use web sockets where you client opens up a socket directly to the server and the server just sends you new data when there is new data.

In either case, you need a back-end server that can handle lots of simultaneous, but mostly idle connections.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • thanks for the answer i will go throw you links but what you say about using nodeJs in my case? will it help? – Peeyush Jan 26 '12 at 02:58
  • @Peeyush - I don't know node.js. It's certainly not necessary. – jfriend00 Jan 26 '12 at 03:19
  • Don't use long polling. Use websockets. – Raynos Jan 26 '12 at 11:51
  • @Raynos - Websockets is obviously favored if present, but it is not fully implemented everywhere (not present in IE9, for example), so one has to test what capabilities are present before deciding which technique to use. See http://caniuse.com/websockets for some info. There are many other references on browser support compatibility available with a [Google search](https://www.google.com/search?sourceid=chrome&ie=UTF-8&q=websockets+browser+support). – jfriend00 Jan 26 '12 at 14:55
0

Node can definitely be useful for this, but it is not necessary as jfriend00 says. Take a look at socket.io for an example of "push" notifications from your server to the browser. Since you don't have any experience with node.js, it would depend on the application as to whether I would recommend it as the solution or not. If this is just a hobby project for educational purposes I would say go ahead, tinker away. If this is for a production system and you're not already using node.js in other capacities, I say stick with something simple like the setInterval method you mentioned in your question.

Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76
0

Try googling socket.io / now.js, it will solve your problem easily by pushing to the client using websockets, and fallback gracefully to flash sockets. You do not have to port all your code to node.js, instead, use it alongside with your original code. Also note that if the interval is originally something like 5 minutes, you'd be better off using setInterval ajax. Websockets is best for (near) realtime communication (with a small latency within seconds, eg. chat).

TiansHUo
  • 8,509
  • 7
  • 45
  • 57