Question

I'm new to it but I really like the idea of long polling.
After reading about it I would implement it like so:

  1. Client requests Server (AJAX)
  2. Server is polling DB for change
  3. When change is recognized or after Time-out Server respond

My problem with this is, that for every request a thread is started on the server plus every thread polls the DB.

Wouldn't it be more effective to run one single Thread on the Server that accepts all incoming Requests? like so:

  1. Client requests Server (AJAX)
  2. Server adds the Request to a queue (no response)
  3. Worker-Thread polls DB for changes related to all requests in queue
  4. When changes are recognized server returns the result for corresponding request

Advantage of this is, that the DB is polled much less and all requests are handled in one thread (less Memory consumption)

My Question:
Is this possible / are there Implementations or Frameworks for that?
(Our Project hasn't started jet so it doesn't matter whether we use PHP or ASP.NET for this)

TIA for your suggestions =)

Was it helpful?

Solution

OTHER TIPS

I can't help you with every request starting a new thread - I don't know if there is a way to have a single thread handle all requests, or if that is a practical solution.

You can have one thread responsible for the database polling - and I highly recommend this because there is no need to have multiple threads querying the exact same data concurrently.

Make use of the Application object. IE Application["ThreadActive"] = true;

if (!Application["ThreadActive"])
ThreadPool.QueueUserWorkItem(new WaitCallback (DBWatcher), objectdata);

Have the DBWatcher method loop itself internally for X hours, turn Application["ThreadActive"] to false, and then end the thread. While this thread is running, have it store the data it retrieves in a Cache object. The threads (or single thread, if you can manage it) will watch the Cache and respond to the long-polls accordingly.

I hope you can find an even more efficient solution, but at least this will limit you to having one open connection with your database instead of who-knows-how-many.

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