I am making a small voting system in python and me plan was to have clients that ask the user for there vote and send it off using pubnub. I then wanted to make an application that will recive the votes and count them all up but I cannot find a way to receive the messages. Am I doing this right or is there a better way thanks for your time.
Asked
Active
Viewed 276 times
1
-
You have a few options. It is recommended to use [PubNub Storage and Playback](http://www.pubnub.com/how-it-works/storage-and-playback/) via your server to aggregate voting totals. You'll grab the history on the channel and tally the vote totals. `pubnub.history({ channel : "...", callback : function, start : "TIMETOKEN" })`. Using PubNub History method is the easiest option. – Stephen Blum Nov 01 '14 at 01:24
1 Answers
1
PubNub Voting Tally with Python
You have a few options. It is recommended to use PubNub Storage and Playback via your server to aggregate voting totals. You'll grab the history on the channel and tally the vote totals.
PubNub PIP Package
pip install pubnub
Tally Votes in Python
from Pubnub import Pubnub
## Init PubNub
pubnub = Pubnub( publish_key="demo", subscribe_key="demo", ssl_on=False )
## Total Votes
last_tt = 0
total_votes = 0
vote_chan = "my_vote_channel"
results_chan = "my_vote_channel.results"
## Tally Callback Function (Sum up the Votes...)
def tally(response):
print(response)
total_votes += len(response[0])
last_tt = response[1]
## Loop Continuously on the last known TIMETOKEN
pubnub.history({ channel : vote_chan, callback : tally, start : last_tt })
## Periodically Publish to the Results Channel
pubnub.publish({ channel : results_chan, message : { "total" : total_votes } })
Using PubNub History method is the easiest option.

Stephen Blum
- 6,498
- 2
- 34
- 46