Send live notification without repeating

Asked

Viewed 88 times

0

I have a code, I wish to make a notification system in live push, here is my code:

  <script>
        Notification.requestPermission();
        function requisitar(){
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
             xmlhttp=new XMLHttpRequest();
        }
        else 
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function()
        {
            var text=xmlhttp.responseText;
            if (xmlhttp.readyState==4 && xmlhttp.status==20)
            {
                var notification = new Notification("Site DaviDEV", {
                    icon: 'http://i.imgur.com/cWk851f.png',
                    body: text
                });
                notification.onclick = function() {
                    window.open("<?php echo $url; ?>");
                }
            }
        }
        xmlhttp.open("GET","/notificacao/notificacao.txt",true);
        xmlhttp.send();



    }

    window.setInterval(requisitar, 1000);
    </script>

The more he can read the file and send the notification, the more he has a problem: He keeps sending several notifications, like spam, I want something that only sends one to each person, what can I do?

  • The reason for "spam" is window.setInterval, try to remove or use a window.setTimeout... I just didn’t understand why use if (window.XMLHttpRequest) when the Browser is recent enough to make the Notification API available.

  • The more I want live, for exeplo I edited the text there, everyone sees. then it goes away, if I edit again it goes and announces the new.

  • Can you explain the question better? what do you mean "I edited the text there"? ( and by the way, I think you want to have 200 here xmlhttp.status==20))

1 answer

0


You will need to use some logic to identify the user and if you have already sent the message to him on the server. The simplest way would be to store a list of ips and check if you are

and instead of using a client-side pooling a better implementation would be using websockets.

You can implement the websocket by yourself using socket.io or use some paid service style elasticpush. to communicate with customers without using pooling.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.