October 2018
Intermediate to advanced
556 pages
15h 18m
English
The last thing that we need in order to complete our use case is an HTML page with some JavaScript code to communicate with the server. For the sake of conciseness, we will strip all HTML tags and leave only the minimum that is required to achieve a result, as follows:
<body><ul id="events"></ul><script type="application/javascript">function add(message) { const el = document.createElement("li"); el.innerHTML = message; document.getElementById("events").appendChild(el);}var eventSource = new EventSource("/temperature-stream"); // (1)eventSource.onmessage = e => { // (2) const t = JSON.parse(e.data); const fixed = Number(t.value).toFixed(2); add('Temperature: ' + fixed + ' C');}eventSource.onopen = e => add('Connection ...