Implementing a live voting system
Asked Answered
L

3

9

I'm looking at implementing a live voting system on my website. The website provides a live stream, and I'd like to be able to prompt viewers to select an answer during a vote initiated by the caster. I can understand how to store the data in a mySQL database, and how to process the answers. However:

How would I initially start the vote on the client-side and display it? Should a script be running every few seconds on the page, checking another page to see if a question is available for the user?

Are there any existing examples of a real-time polling system such as what I'm looking at implementing?

Lumberyard answered 7/1, 2011 at 2:47 Comment(0)
B
4

You would have to query the server for a new question every few seconds.

The alternative is to hold the connection open until the server sends more data or it times out, which just reduces (but does not eliminate) the server hits. I think it is called "long polling". http://en.wikipedia.org/wiki/Push_technology

Bioscopy answered 7/1, 2011 at 2:58 Comment(0)
B
1

You could use setTimeout in JavaScript to make AJAX requests each few seconds to check whether there are new questions.

Yes, long polling might be better, but I'm sure it's a bit more complex. So if you are up to the job, go ahead and use it!

Here's a bit more info on the topic: http://www.webdevelopmentbits.com/avoiding-long-polling

Bahia answered 7/1, 2011 at 3:1 Comment(2)
It's not way more complex, you just don't send the response for a while. See #334164.Blum
fine then, a bit more complex. PS: There's a code example of long-polling in the website I posted.Bahia
B
1

You will have to originate the connection from the client-side. The simplest solution is to have the page make an AJAX request every second or so. Web pages don't have to return immediately (they can take 30 seconds or more before responding without the connection timing out). This, opening one connection which doesn't respond until it has something to say, is "long-polling".

Blum answered 7/1, 2011 at 3:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.