Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl: the Markov chain saw
 
PerlMonks  

Perlmonks AJAX Voting

by eric256 (Parson)
on Dec 13, 2007 at 02:15 UTC ( #656735=monkdiscuss: print w/ replies, xml ) Need Help??

So I've started with the existing Greasemonkey script to add AJAX voting to Perlmonks AJAX replacement for voting buttons and added the ability to lookup the new reputation using the XML version of the nodes. It submits once to vote and then a second time to retrieve the reputation of the node you just voted on. This is required because when casting a vote you have to pass the node_id of the page your are voting from. I'm sure that this could be gotten around somehow.

I then started comparing the characters returned for the XML requests versus the characters returned by processing the entire page, and the page load times (these are the only metrics i could think of to start with, hopefully someone has some better ideas?) So I tested with a node and found out how many characters where needed for each vote. On the node tested it cost about 6k characters for the AJAX vote (and two requests) and 66k characters to load the page. The page load time averages 3s and the 2 XML page load times averaged .3 seconds.

Without better metrics it appears that AJAX voting is cheaper as long as you are voting less than 10 times on a page. 10 ajax votes would cost 60k characters and 3 seconds, about the same as loading the entire page once. If the voting limitation is changed then the cost of AJAX votes could be cut in half, or even less if the request could specifiy it only wants the reputation XML.

I was hoping to find that this could significantly enhance server performance. I'm not sure I've found that, but i do beleive that if the average number of votes per page is low (say < 6) then this could result in a great improvment in perlmonks performance.

So is this something we want to look into supporting to some degree? I.e. have a greasemonkey plugin with special XML syntax, or even provide javascript that could be entered into the freenodelet? or is this performance increase insignificant?

I would be interested to see if there are other aspects that could benefit from some AJAX as either a freenodelet code or greasemonkey script. Please express your opinions!

Install - Sorry I couldn't figure out a proper bracket to do the job.


___________
Eric Hodges

Comment on Perlmonks AJAX Voting
Re: Perlmonks AJAX Voting (greasemonkey plugin)
by eric256 (Parson) on Dec 13, 2007 at 02:16 UTC

    • Updated 12-17-07: Added a refresh link to the XP Nodelet (click the @ to get a refreshed count of votes)
    • Updated 12-15-07: removed the need for javascript in a big string, fixed issues with voting booth and picked some better XPath's.
    • Updated 12-14-07: fix error with parent_node_id that caused it to be detected as a bot
    • Updated 12-13-07: use local XMLHttpRequest objects and removed extraneous code.


    ___________
    Eric Hodges
      Greasemonkey isn't needed since PerlMonks already allows you to inject HTML and scripts via the Free Nodelet. Perhaps I'll take a stab at converting the code next week.

      Maybe I'm wrong, but I see that you are using global xmlHttpReq objects in asynchronous requests. This could lead into problems, because a second (and successive) asynchronous request (i.e. a second vote in the page) will overwrite previous ones if they are not finished and you'll never receive a response from them.

      There are some ways of solving this, for example, putting multiple xmlHttpReq objects in an array.

      Sorry if I misread your code

      citromatik

        Thanks for that catch, I updated the code to use local variables, some testing shows that this works now. Guess I never though of anyone voting that fast!


        ___________
        Eric Hodges

      Updated 12-17-07: Added a refresh link to the XP Nodelet (click the @ to get a refreshed count of votes)


      ___________
      Eric Hodges
Re: Perlmonks AJAX Voting
by KurtSchwind (Hermit) on Dec 13, 2007 at 02:42 UTC

    I think that AJAX is the way to go for the voting buttons. I also hope we can reduce the cost of AJAX from 6K to something closer to 1K.

    I'm not sure how other's vote, but I kind of mill about and think about things and sporadically vote, so this would speed up the experience for me, as well as reduce the over-all load to PM.

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re: Perlmonks AJAX Voting (PM attacks!!!)
by eric256 (Parson) on Dec 13, 2007 at 14:35 UTC

    Hehe, I think I convinced PM that I'm a bot. When I used the last of my votes it says that I lost 27XP. I'm not to wrapped up in the XP but I do want to make sure that this code isn't getting seen as a bot. If some god could take a look and let me know I would appreciate it.


    ___________
    Eric Hodges

      Thanks to a msg from ysth I discovered the bug that caused it to be detected as a bot. The node_id was getting messed up on pages where the voting booth is shown (which messed me up since i just recently added it to all pages....now if the voting both would go away after you vote!! ;) ) New version updated in the (greasemonkey plugin) sub-thread.


      ___________
      Eric Hodges
Re: Perlmonks AJAX Voting
by ambrus (Abbot) on Dec 13, 2007 at 14:37 UTC

    Is there any way to use something like this, but without one-click voting? That is, I'd like to be able to change my mind about the vote, and only submit it when I click on the vote button at the end of the page.

        That is, I'd like to be able to change my mind about the vote, and only submit it when I click on the vote button at the end of the page.

      I agree -- strange as it may sound.

      If I end up at a node that I think is correct, I may signify my intention to upvote it by clicking on the ++ radio button, and continue by reading the replies. Sometimes I discover the node I originally upvoted is actually dead wrong -- in which case I may choose to change my vote to a downvote, or just decide not to vote on that node at all by navigating away from the page.

      It doesn't happen often, but the flexibility would be very handy. Perhaps this should be user-configurable?

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        I don't think it's really odd. We only have so many votes and sometimes will have to make tough choices on where to spend them, especailly when our remaining vote count is fairly low. It's nice to try and go back the next day and hit some of those nodes I couldn't but I may forget or something may come up that prevents me from hitting PM that day. I also think one-click voting makes accidental or knee-jerk voting more likely.
      Have you used the "null vote"? It adds a radio button labeled +=0 which cancels your vote if you haven't submitted. Look at User Settings, under the miscellaneous section.

      No ajaxy Web 2.0 feel, but it does allow you to change your vote.

Re: Perlmonks AJAX Voting
by Gavin (Canon) on Dec 13, 2007 at 19:51 UTC
    ++ eric256 Neat and really nice idea, I'm with talexb I do change my mind often mid read of an entire node, more often than not.
    It would be better to be able to have second thoughts and not to be commited to vote at first pick.

    Perl and AJAX will I'm sure have a long relationship in the future, they seem made for each other.
Re: Perlmonks AJAX Voting
by aufflick (Deacon) on Dec 17, 2007 at 01:31 UTC
    Brilliant. I've been using the ajax voting for some time and it's nice to see the result again.

    I added the following patch in the appropriate place so that the ++ and -- don't run together:

    up_vote.innerHTML = "++"; voting_both.appendChild(up_vote); + + var spacer = document.createElement("span"); + spacer.innerHTML = "&nbsp;"; + voting_both.appendChild(spacer); var down_vote = document.createElement('a'); down_vote.setAttribute("href",'javascript:void(0);');

      Thanks for this patch, I've implemented it in the code here.


      ___________
      Eric Hodges
Re: Perlmonks AJAX Voting
by aufflick (Deacon) on Dec 20, 2007 at 01:18 UTC
    Hi Eric,

    Your latest version has a minor typo. Line 104 should be:

    if (xpNodelet) {

    instead of:

    if (xpNodelet)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://656735]
Approved by Arunbear
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2014-04-21 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls