What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 122 Stack Exchange communities.

I'm wondering how SO is able to display the "New answers have been posted" message when I'm typing an answer for a question. Does my browser send an AJAX request to the SO servers, or does SO send a trigger back to my browser?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

SO can't send trigger to the browser due to the very nature of web.

SO is using AJAX indeed, here is part of the code responsible for that nice message:

var count = parseInt(data.message);
if(count > 0) {
    var msg2 = count + " new answer" + (count == 1 ? " has" : "s have") + " been posted - ";
    msg2 += '<a onclick="heartbeat.answer.update()">load new answers.</a>';
    notify.show(msg2, notifyMessageTypeId);
    hasBeenNotified=true
}

The server is sending back the amount of new answers and if positive, the message is displayed.

If you really want, shouldn't be too hard to find the actual code invoking the AJAX request. :)

EDIT: This is not relevant anymore due to real time updates using web sockets:
New Feature: real time updates to questions, answers, and inbox

share|improve this answer
    
Does the the browser poll the server at regular intervals ? –  Subhra Jul 5 '12 at 8:24
    
@Subhra not anymore, now it's using web sockets which is new feature and allows two way communication. Not sure what happens in case of browser not supporting it (currently only Chrome does) so I asked the developers and waiting for answer. –  Shadow Wizard Jul 5 '12 at 8:38
    
Please let me know if you get the answer . I need to implement similar thing and the solution should be cross browser. –  Subhra Jul 5 '12 at 8:51
    
@Subhra so use AJAX mechanism like it was used here, it will take long time until web sockets become cross browser. –  Shadow Wizard Jul 5 '12 at 9:09
add comment

The former; it sends an AJAX request to /posts/POSTID/edit-activity-heartbeat every 45 seconds or so, and gets back info like if any new answers have been posted, or if the post has been deleted (so the answer button can be grayed out)

share|improve this answer
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .