Running our solution

It's time to see our code in action. Ensure that you have nsqlookupd, nsqd, and mongod running in separate terminal windows with the following:

nsqlookupd
nsqd --lookupd-tcp-address=127.0.0.1:4160
mongod --dbpath ./db

If you haven't already done so, make sure the twittervotes program is running too. Then, in the counter folder, build and run our counting program:

go build -o counter
./counter

You should see a periodic output describing what work counter is doing, such as the following:

No new votes, skipping database update
Updating database...
map[win:2 happy:2 fail:1]
Finished updating database...
No new votes, skipping database update
Updating database...
map[win:3]
Finished updating database...

Tip

The output will, of course, vary since we are actually responding to real, live activity on Twitter.

We can see that our program is receiving vote data from NSQ and reports to update the database with the results. We can confirm this by opening the MongoDB shell and querying the poll data to see whether the results map is being updated. In another terminal window, open the MongoDB shell:

mongo

Ask it to use the ballots database:

> use ballots
switched to db ballots

Use the find method with no arguments to get all polls (add the pretty method to the end to get nicely formatted JSON):

> db.polls.find().pretty()
{
  "_id" : ObjectId("53e2a3afffbff195c2e09a02"),
  "options" : [
  "happy","sad","fail","win"
  ],
  "results" : {
    "fail" : 159, "win" : 711,
    "happy" : 233, "sad" : 166,
  },
  title" : "Test poll"
}

The results map is indeed updated, and at any point in time, it contains the total number of votes for each option.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.117.191