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>