This discussion is archived
13 Replies Latest reply: Feb 8, 2013 3:32 PM by EJP RSS

how does facebook comments work? I mean if i have to do in j2ee.

Murray9654 Newbie
Currently Being Moderated
For example on facebook when we post a comment, it appears right next to the earlier comment immediately. how does this happen? I don't think the entire page is reloading. I even don't think part of the page is refreshed. Is it Ajax? If so what are the steps involved in achieving this? If not Ajax, what is it and how is it being done?

one way to do this is
write a comment and submit. when submit make an ajax call to the server. On the server capture the param and store it into the database. After storing in the database retrieve the stored messages based on the time stamp and send them as response. one the client (browser) receives the response as HTML, add this HTML is appended next to the recent message.

I feel this is good but is there a better way than this?
  • 1. Re: how does facebook comments work? I mean if i have to do in j2ee.
    Kayaman Guru
    Currently Being Moderated
    Muralidhar wrote:
    Is it Ajax?
    Yes. Unless it's websockets, but I doubt that.
    one way to do this is
    write a comment and submit. when submit make an ajax call to the server. On the server capture the param and store it into the database.
    Sounds good.
    After storing in the database retrieve the stored messages based on the time stamp and send them as response.
    Send all the messages back? why? There's no need to hit the database anymore.
    one the client (browser) receives the response as HTML, add this HTML is appended next to the recent message.
    Not HTML, more likely JSON.
  • 2. Re: how does facebook comments work? I mean if i have to do in j2ee.
    Murray9654 Newbie
    Currently Being Moderated
    After storing in the database retrieve the stored messages based on the time stamp and send them as response.
    Send all the messages back? why? There's no need to hit the database anymore.
    I don't mean all the messages. I mean to say the stored message. If there is no need to hit the database what should we do? do you mean just return the message that we stored as response to the client because it is the message that we have just stored.
    >
    one the client (browser) receives the response as HTML, add this HTML is appended next to the recent message.
    Not HTML, more likely JSON.
    why JSON and why not HTML? I mean if it is a jsp that responds then it will be HTML right. If the response should be JSON do we have to use any api on server? how to send JSON as response from server?

    Edited by: Muralidhar on Feb 6, 2013 9:08 PM

    Edited by: Muralidhar on Feb 6, 2013 9:16 PM
  • 4. Re: how does facebook comments work? I mean if i have to do in j2ee.
    Kayaman Guru
    Currently Being Moderated
    Muralidhar wrote:
    I don't mean all the messages. I mean to say the stored message. If there is no need to hit the database what should we do? do you mean just return the message that we stored as response to the client because it is the message that we have just stored.
    Well is the message any different if you fetch it back from the database? Sure it might have a generated primary key and so forth, but do you really need that?
    why JSON and why not HTML?
    It depends on the content you're sending.
    I mean if it is a jsp that responds then it will be HTML right.
    Not necessarily, a JSP can respond in any content type.
    If the response should be JSON do we have to use any api on server?
    Well, you might want to use a ready made lib to convert your objects to JSON.
    how to send JSON as response from server?
    With the appropriate content-type.
  • 5. Re: how does facebook comments work? I mean if i have to do in j2ee.
    r035198x Pro
    Currently Being Moderated
    You use JSON because the structure of the page has not changed so no need to send html from the server, just the data. Use javascript templating functions like underscore.js to build the new html structure. If the comments are automatically updated when someone else has posted then you don't need to rebuild all the data, just append the new comment.
  • 6. Re: how does facebook comments work? I mean if i have to do in j2ee.
    Murray9654 Newbie
    Currently Being Moderated
    I don't mean all the messages. I mean to say the stored message. If there is no need to hit the database what should we do? do you mean just return the message that we stored as response to the client because it is the message that we have just stored.
    Well is the message any different if you fetch it back from the database? Sure it might have a generated primary key and so forth, but do you really need that?
    well can you help me out in figuring what has to be done. I mean as you said we don't need primary key and we don't need to hit the database. Ok but how it should be done? If i understand this, it will really help me.
    >
    why JSON and why not HTML?
    It depends on the content you're sending.
    if i am not wrong if is arrays,list or hashmap or a simple string then i think json is good.
    If the response should be JSON do we have to use any api on server?
    Well, you might want to use a ready made lib to convert your objects to JSON.
    I think if i have to return json then i dont need a jsp. is it right?
    >
    how to send JSON as response from server?
    With the appropriate content-type.
    json is just a string so "text" content type will do. am i right?
  • 7. Re: how does facebook comments work? I mean if i have to do in j2ee.
    Murray9654 Newbie
    Currently Being Moderated
    r035198x wrote:
    You use JSON because the structure of the page has not changed so no need to send html from the server, just the data. Use javascript templating functions like underscore.js to build the new html structure. If the comments are automatically updated when someone else has posted then you don't need to rebuild all the data, just append the new comment.
    you are saying the page structure has not changed so json and you are also suggesting a library to rebuild the new html structure. I mean when the page is not changing and if it is just a string that i have to append next to the messages that are already present why should i need to rebuild html structure?

    What do you mean by automatic updates when some one else posted? automatically means how?
  • 8. Re: how does facebook comments work? I mean if i have to do in j2ee.
    r035198x Pro
    Currently Being Moderated
    Muralidhar wrote:
    What do you mean by automatic updates when some one else posted? automatically means how?
    Like you are looking at the page and it's contents change without you making a refresh. Common options are polling (discouraged) and these days websockets.
  • 9. Re: how does facebook comments work? I mean if i have to do in j2ee.
    Murray9654 Newbie
    Currently Being Moderated
    What do you mean by automatic updates when some one else posted? automatically means how?
    Like you are looking at the page and it's contents change without you making a refresh. Common options are polling (discouraged) and these days websockets.
    i mean this can be done with ajax as well right. the page still stays and we can make a request and get response using ajax and can use the response to add the content to html using javascript. why do we need websockets for this?
  • 10. Re: how does facebook comments work? I mean if i have to do in j2ee.
    r035198x Pro
    Currently Being Moderated
    I mentioned websockets when you asked about automatically refreshing the content even if the user has not refreshed the page (or posted a comment). Yes you can use AJAX but then you have to keep asking the server if there are new comments to update even if there is no new data. Websockets do not need the polling because the server can send a message to the client. The choice really comes down to how many users you are likely to have connected at a time and how frequently your data changes. AJAX is best for very frequent changes because then most poll hits actually return data and are not worthless
  • 11. Re: how does facebook comments work? I mean if i have to do in j2ee.
    Murray9654 Newbie
    Currently Being Moderated
    I don't mean all the messages. I mean to say the stored message. If there is no need to hit the database what should we do? do you mean just return the message that we stored as response to the client because it is the message that we have just stored.
    Well is the message any different if you fetch it back from the database? Sure it might have a generated primary key and so forth, but do you really need that?
    well can somebody help me out in figuring what has to be done. after storing the message in the database how do i send the message back to the client? as per above discussions after storing in the database we dont need primary key and we also dont need to hit the database to retrieve the stored message so how do i display this message in the browser after storing in the database without any additional hit to the database?
  • 12. Re: how does facebook comments work? I mean if i have to do in j2ee.
    r035198x Pro
    Currently Being Moderated
    Either
    1.) Return all the comments to the client after a successful save so the client can display all the comments
    2.) Return a count of the total comments in the database to the client. The client knows how many comments it's currently displaying so either
    a.) There were no new comments so the client simply appends the one it just sent to the server to it's view
    b.) The server now has more comments than the client so the client makes a further request to the server asking for only the new comments that it doesn't have which it then just appends to the view.
  • 13. Re: how does facebook comments work? I mean if i have to do in j2ee.
    EJP Guru
    Currently Being Moderated
    Moderator action: This wasn't originally and has not become a JSP or JSTL question, or even a Java question, just a very general web-app question. Off topic. Locking.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points