Skip to content

Instantly share code, notes, and snippets.

@yottta
Created March 25, 2019 13:27
Show Gist options
  • Save yottta/0ebe18cb3b49079c6c3c7c3eb70db264 to your computer and use it in GitHub Desktop.
Save yottta/0ebe18cb3b49079c6c3c7c3eb70db264 to your computer and use it in GitHub Desktop.
Medium article about WebSockets throughout Varnish - initial web client
<!DOCTYPE html>
<html>
<head>
<title>Products</title>
</head>
<body>
<p>Product List:</p>
<ul id="products">
</ul>
<script>
const httpGET = new XMLHttpRequest();
httpGET.open("GET", "http://127.0.0.1:8080/product");
httpGET.send();
httpGET.onloadend = (e) => {
var products = JSON.parse(httpGET.responseText);
products['products'].forEach(function(el) {
existingContent = document.getElementById("products").innerHTML
document.getElementById("products").innerHTML = existingContent + ("<li>" + el + "</li>");
});
}
let socket = new WebSocket("ws://127.0.0.1:8080/ws")
socket.onopen = () => {
console.log("Successfully websocket connected")
}
socket.onclose = (event) => {
console.log("Socket closed connection", event)
}
socket.onerror = (error) => {
console.log("Socket error: ", error)
}
socket.onmessage = (msgEvent) => {
console.log("Message received", msgEvent.data)
existingContent = document.getElementById("products").innerHTML
document.getElementById("products").innerHTML = existingContent + ("<li>" + msgEvent.data + "</li>");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment