How to create a real-time notification system similar to Stack Overflow?

28

I am developing a Help Desk system, and would like some ideas on how to create a notification system similar to Stack Overflow itself, whenever any new support is registered.

I want to use PHP, MySQL and jQuery only.

Should I use window.setTimeout to check in the bank from time to time if a new call is registered?

    
asked by anonymous 16.03.2014 / 20:21

4 answers

16

I just answered a similar question, just talking about a chat.

The options you have are as follows:

To implement the chat:

  • The option that will give you the widest scope in terms of browsers, is called long-polling , but it will also give you more work. See more about the technique: long-polling
  • Another option that will be easier is to use websocket , and create a server connection, which will be able to send messages when any chat event occurs ... type send a message but in that case the server will need to be able to receive the client connection via websocket (you will have to be able to wait for connections ... just search for "php websocket" on google that pop up some libraries).
17.03.2014 / 18:30
11

Dude, DO use ajax with setInterval. See, imagine that every 1 minute the system will make a request to the server and return a response to the browser, even if there is no change in the conversation. If there are multiple clients, it will overload the system with ajax requests, which would be a waste of resources.

Look for PHP + LONG POLLING. Long Polling works differently than ajax, the connection stays open waiting for a response from the server.

The ideal would be to implement this part in nodejs, since it is more suitable for real time applications.

    
21.03.2014 / 18:47
0

You can use AJAX:

  • Create a function that will contain the AJAX object and will make the request.
  • Then call this function with window.setInterval() .

index.php file :

<html>
   <head>
      <script>
        function requisitar(){
            var xmlhttp;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                 xmlhttp=new XMLHttpRequest();
            }
            else 
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","myfile.txt",true);
            xmlhttp.send();
        }

        window.setInterval(requisitar, 1000);
</script>
   </head>
   <body>
       <div id="myDiv">Foo bar</div>
   </body>
</html>

Myfile.txt file, which can also be named myfile.php :

Charles
    
16.03.2014 / 22:08
0

Have you ever thought about using some kind of Pusher notification system such as Pusher or PubNub ?

You use timeout you will be doing Long Polling and this type of update uses a lot more feature.

My suggestion for you would be to take a look at the best option:

17.03.2014 / 19:41