0

I am making a web app that requires users to get push notifications from the server. I am thinking of using webSockets to do it. I have a basic example working. The server is in go lang. I was just wondering if there is a better way of implementing push notifications?

<html>
<head></head>
<body>
    <script type="text/javascript">
        var sock = null;
        var wsuri = "ws://192.168.0.165:8080";

        window.onload = function() {

            console.log("onload");

            sock = new WebSocket(wsuri);

            sock.onopen = function() {
                console.log("connected to " + wsuri);
            }

            sock.onclose = function(e) {
                console.log("connection closed (" + e.code + ")");
            }

            sock.onmessage = function(e) {
                console.log("message received: " + e.data);
                document.getElementById("p1").innerHTML = e.data;
            }
        };

        function send() {
            var msg = document.getElementById('message').value;
            sock.send(msg);
        };
    </script>
    <h1>WebSocket Echo Test</h1>
    <form>
        <p>
            Message: <input id="message" type="text" value="Hello, world!">
        </p>

        <p id="p1">MSG!</p>
    </form>
    <button onclick="send();">Send Message</button>
</body>
</html>
amanda45
  • 535
  • 10
  • 29
  • SSE is an option - see the answer here https://stackoverflow.com/questions/5195452/websockets-vs-server-sent-events-eventsource – Jaromanda X Aug 12 '17 at 02:18

1 Answers1

0

For push notification on frontend you can use FIREBASE https://www.npmjs.com/package/firebase:

<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
<script>
  // Initialize Firebase
  // TODO: Replace with your project's customized code snippet
  var config = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
    storageBucket: "<BUCKET>.appspot.com",
    messagingSenderId: "<SENDER_ID>",
  };
  firebase.initializeApp(config);
</script>
аlex
  • 5,426
  • 1
  • 29
  • 38