How to make "Stock price label updating" kind of feature?


I need to make a feature like Yahoo finance stock price updating, so that the label is “ticking” every 5 seconds, for example. I understand the basic idea from the following post that :

  1. make a thread updating the data every 5 seconds
  2. find all application instances
  3. call an user defined function in the application to update the label

Here is the post:

My problem now is that I need this feature in a hurry and I am afraid of doing something stupid even it is working (eg, performance problem). So has anyone already made similar kind of feature? could you share some example code? What is the right way to get all application instances?

Many thanks!

The simplest would be to use
the refresher add-on

Add it to the page you want to refresh the values (it will not display anything but it needs to be somewhere), set it to refresh every 5 seconds.
Add a refresh listener and on the refresh event, update your values.

As long as updating values is quick, this saves you all the problems of running different threads and synchronizing data access.

Thanks of your solution! Am I understand correct that this solution is to let client send a request every 5 seconds to server? If I have some 1000 users or even more, would I have to upgrade my server hardware? I thought one thread in server updating every 5 seconds and distribute the data to application instance would be most cost efficient solution. The data retrieved is an analytically database query result. This is also the reason why I want to minimize the query frequency.


Yes using a thread on the server side will update the values on the server side but you still will need to push the updated values to the clients as well.

Basically you have two options here:

  1. Use the refresher or similar to poll for changes every 5 seconds. This means there will be a request every 5 seconds regardless if the value has changed or not.
  2. Use one of the push solutions available in the directory. This means the value is only updated to the clients when it changes so it is more efficient but implementation is a bit harder then using the refresher.

You understand correctly how it works.
On each request, the server will only send changes back. So if only few elements are updated on a page between 2 calls, the process is really quick.

You can also look in the ICEPush and another one which I don’t remember the name for those you will have to manage a thread pool, with one thread per user running continuously on the server for refresh. That thread then do the changes and call push to send the changes to the client.
This also require the server to keep a communication channel continuously open with the user and you will also need to manage the freeing of the thread when the client disconnect / change page / crashes / …

If you need updates as soon as possible pushing is the best. If you want only updates once in a while, polling (refresher addon) is the best :

  • don’t care about creating threads
  • don’t care about making the application thread safe
  • don’t care about freeing threads (clients just stop calling)
  • no thread pool and no background thread running continuously
  • only a quick call every now an then

But if the process to update the data is really long, the user will see it’s browser freeze (until the server is done updating) so in this case pushing is best with it’s detached threads.

It all depends on your specific case. Refresher is easy to set up, but not adapted to all use.

If you can afford the data being possibly a few seconds late, one relatively simple but efficient solution would be to have one thread update a shared server-side cache every 1-5 seconds, and use e.g. Refresher to fetch the data. Your refresh listener can then update the UI components, or tell them they need to be refreshed if they fetch the data directly from the cache.

A few small helper classes for handling the updates etc. can go a long way if you have many such cases.

Thanks for comments! I think I will take Henri’s solution plus the refresh add-on suggested by Mathias. My plan is:

  1. one thread running on server, every 5 seconds run the analytical query and save result to memory
  2. client side refresher send request to server to get the latest saved result from memory (so no database connection)

The result in memory contains only 8 objects in my case, and the query takes less then 5 seconds (in production, I hope) So this combination would be quite efficient.

The PUSH solution sounds like stateful messaging, I am a bit afraid of the performance once there are many instance (users) on the same server.

Thanks again!