7 Replies - 9103 Views - Last Post: 14 July 2011 - 08:03 AM Rate Topic: -----

#1 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Polling database every x seconds

Posted 12 July 2011 - 05:49 AM

Scenario:

I am building an application for a sports club where in all players must use it to get announcements and to get latest news, reports etc.

So I thought about having a players online type system where other players can see what players are currently online so they can chat to them if they would like.

Implementation:

.net 4 Entity Framework 4 using MySQL 5 database backend with InnoDb tables for relationships

When a player logs in to the system, a flag (Online) gets set to true.

Currently I am using a timer to poll the database every 5-10 seconds to see what users are online (Online flag) to update a list on the application.

When a chat window is initiated between two players, it polls every 3 seconds

I do not have access to a dedicated windows box currently otherwise I would have gone with the TCP Client / Server type setup.

Questions:

1) Is this good practice to poll a database table with such a short timescale?
2) Is there a better way to acheive this information regarding who is online / chat with just a database?

Any thoughts / suggestions appreciated :)

This post has been edited by batesy3k: 12 July 2011 - 05:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Polling database every x seconds

#2 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: Polling database every x seconds

Posted 13 July 2011 - 03:24 PM

The timer method is functional but don't you have a method that un-flags the user as online when they hit sign out? Unless you are worrying over say, their internet connection failing or them closing the window. Then a timer would seem practical (to my understanding).
Sorry if this didn't help that much saw this had zero replies tried to help you out :D

This post has been edited by Milto: 13 July 2011 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5804
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Polling database every x seconds

Posted 13 July 2011 - 05:56 PM

View Postbatesy3k, on 12 July 2011 - 08:49 AM, said:

1) Is this good practice to poll a database table with such a short timescale?


Not ideal, but possible. Databases are designed to have many people asking many questions simultaneously. So, having one person continuously asking the same question isn't so bad. Of course, if everyone is continuously asking that question, you can get contention.

A better approach would be to have just one global object ask the question over the interval and tell it to anyone that asks, any time they ask.

View Postbatesy3k, on 12 July 2011 - 08:49 AM, said:

2) Is there a better way to acheive this information regarding who is online / chat with just a database?


Playing off the global object... you write a middle tier that has state. It needn't have a database a all, really. When a client logs in, a client object is created for them. When an event occurs, all live client objects are notified. The state is stored centrally, updated on events, and can be queried as needed.
Was This Post Helpful? 0
  • +
  • -

#4 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Polling database every x seconds

Posted 14 July 2011 - 01:11 AM

@Milto Yeah the flag gets set to false when the application closes.

Let me see if I have got it correct:

So does that mean that the "global object" such as ChatManager does all the database querying etc and hold's a list of clients.

The client's subscribe to events within the ChatManager so when (for example) a new chat message is sent, the appropiate client is notified and then acts upon the information sent via the event?

Does this manager have to be written server side as I don't have a dedicated windows box at the moment to write a service for example thus going the database route which is hosted on a web hosting package?

This post has been edited by batesy3k: 14 July 2011 - 03:32 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5804
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Polling database every x seconds

Posted 14 July 2011 - 04:24 AM

View Postbatesy3k, on 14 July 2011 - 04:11 AM, said:

So does that mean that the "global object" such as ChatManager does all the database querying etc and hold's a list of clients.


Yep, you got it.

View Postbatesy3k, on 14 July 2011 - 04:11 AM, said:

The client's subscribe to events within the ChatManager so when (for example) a new chat message is sent, the appropiate client is notified and then acts upon the information sent via the event?


An event model makes sense. Though the ChatManager can also simply call a method in each of it's current clients for notification.

View Postbatesy3k, on 14 July 2011 - 04:11 AM, said:

Does this manager have to be written server side


Um, yeah. If you can dedicate a server to a database, and you plan to write server side code in C#, then it's reasonable that you have a server at your disposal.

I would look into WCF. Indeed, if it's a chat type application you're after, such are usually part of the example code for such frameworks. You can find more here: http://www.google.co...wcf+chat+server
Was This Post Helpful? 1
  • +
  • -

#6 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Polling database every x seconds

Posted 14 July 2011 - 05:37 AM

Yeah that does make good sense and would be the way I go once I get a dedicated hosted Windows box :) Cheers for the input and thoughts!

Just for the mean time, does my method of polling directly to the database with EF make sense? Or should I create some php api scripts which my app connect to, to do the database work and send the information back?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5804
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Polling database every x seconds

Posted 14 July 2011 - 07:10 AM

constantly bugging the database should work fine. That's what they're their for.
Was This Post Helpful? 0
  • +
  • -

#8 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Polling database every x seconds

Posted 14 July 2011 - 08:03 AM

Thanks for your time and discussions :) I'll report back if I need anything else !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1