Php and JQuery - How to Make Changes to a Page Visible from Computers accessing the Site Page

I have a simple social networking site wherein there's a post function that saves the post and displays it as a new div within a list at the top of the unordered list, basically, it acts like a timeline. The problem is that, the current code that I have only works for the current PC that is viewing the page. What is the proper way of using JQuery .ajax() for handling this scenario ? For example, there are two PC's, PC 1 and PC 2. PC 1 posted something and they saw it display on the timeline. Now, PC 2 can't see that the timeline is updated.

How can I make the two PC's be able to see the post made by each other without refreshing the page? For now, this is my ajax code for adding the post and then displaying it to the index page.

$(document).ready(function(){
$("#someButton").click(function(){
    $.ajax({
        url: "/someDir/rest/post/",
        type: "POST",
        data: {postText: $("#someTextArea").val()},
        success: function(data){
            var requestData = data;
            $.ajax({
                url: "/someDir/rest/update/",
                type: "POST",
                data: {latestId: requestData},
                success: function(data){
                    $(".listContainer").prepend(
                        "<li>" +
                        "<div class = 'someClass'>" +
                        "<span>" + data[0].userId + "</span><br>" +
                        "<span>" + data[0].postText + "</span><br>" +
                        "<span>" + data[0].postDate + "</span><br>" +
                        "</div>" +
                        "</li>"
                    );
                }
            });
        }
    });
});

});

1 answer

  • answered 2018-02-13 02:30 Draconid

    there are 2 ways you can do it.

    1. setTimeInterval to keep calling server for updates every few seconds. it is easy to do, but causes loading to the server.
    2. using websocket to make the server notify frontend about the update, it is a little more difficult to do, where you will need a websocket server too.