SkyPunch
SkyPunch

Reputation: 324

Check database for changes via long polling

Im creating a chat app in ASP.NET MVC3. im using long polling and AsyncController to do so

when a user posts a chat its saved in database , to retrieve should i constantly check database for change in record or after definite interval or is there an better/ efficient way of doing it

i came across this question but could not get a usable answer.

Upvotes: 5

Views: 2738

Answers (3)

rpgmaker
rpgmaker

Reputation: 790

You can give PServiceBus(http://pservicebus.codeplex.com/) a try and here is a sample web chat app(http://74.208.226.12/ChatApp/chat.html) running and does not need database in between to pass message between two web clients. If you want to persist data in the database for logging sake, you can always subscribe to the chat message and log it to database.

Upvotes: 0

petebowden
petebowden

Reputation: 910

Since you're already using long polling and an asynccontrolller, why not create a message pool? Take a look at this solution.

In a nutshell, instead of just writing the updated chat to the database, you should also stick it in some sort of queue. Then each user's async thread is listening to that pool waiting for a message to appear. When one appears return the data to the user through your normal operation. When all listening threads have picked up the message it can be removed from the queue. This will prevent you from having several threads hammering your database looking for a new message.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

You may take a look at SignalR for an efficient way. Contrary to the standard polling mechanism (in which you are sending requests at regular intervals to check for changes), SignalR uses a push mechanism in which the server sends notifications to connected clients to notify them about changes.

Upvotes: 4

Related Questions