Server sent event supports the server push from server to browser only.
In Javascript
var sse = null;
function runNow() {
if (!sse) {
sse = new EventSource('/sse_stream');
sse.addEventListener('message',
function (e) {
document.getElementById('lbl1').innerText = e.data;
});
}
}
function stop() {
if (sse) {
sse.close();
sse = null;
}
}
Reference:
https://www.html5rocks.com/en/tutorials/eventsource/basics/
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Monday, 27 May 2019
Web worker
Summary of web worker
The JavaScript to that create a web worker
This script will be added to the HTML page like a normal JS file.
var w;
var lbl1;
function onStart() {
if (w) {
return;
}
w = new Worker('demo_worker.js');
lbl1 = $('#lbl1');
w.onmessage = function (e) {
if (e.data.t == 'number') {
lbl1.text('running value=' + e.data.i);
}
else {
console.log(e.data)
}
}
}
function onStop() {
if (w != null) {
w.terminate();
w = null;
}
}
function onSayHelo() {
if (w) {
w.postMessage('helo from tab...' + (new Date()));
}
}
The web worker Javascript file "demo_worker.js"
var i = 0;
function timedCount() {
i = i + 1;
postMessage({
t: 'number',
i: '<b>'+ i + '</b>',
ts: new Date()
});
setTimeout("timedCount()", 1000);
}
function testAjax() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (e) {
if (xhr.readyState == 4 && xhr.status == 200) {
// upon loaded succesfully, pass it to the HTML page.
postMessage({
t: 'data',
data: xhr.response
});
}
}
xhr.open('get', 'test-data.txt');
xhr.send();
}
// message from the HTML page.
self.onmessage = function (e) {
console.log('worker...', e.data)
}
timedCount();
setTimeout(testAjax, 3000)
- Every web worker is a thread on it's own!! Instantiate too many worker instances might degrade your computer performance.
- The worker will not be able to access the HTML page.
- All data exchange/communication must be done through postMessage().
- With web worker, the communication between the HTML/JS and the worker will be more complex.
- Good thing is that process in the web worker won't affect the HTML page update/redraw.
The JavaScript to that create a web worker
This script will be added to the HTML page like a normal JS file.
var w;
var lbl1;
function onStart() {
if (w) {
return;
}
w = new Worker('demo_worker.js');
lbl1 = $('#lbl1');
w.onmessage = function (e) {
if (e.data.t == 'number') {
lbl1.text('running value=' + e.data.i);
}
else {
console.log(e.data)
}
}
}
function onStop() {
if (w != null) {
w.terminate();
w = null;
}
}
function onSayHelo() {
if (w) {
w.postMessage('helo from tab...' + (new Date()));
}
}
The web worker Javascript file "demo_worker.js"
- This file will run in it's own thread.
- All data exchange must be done through postMessage().
- The worker is able to execute AJAX call.
- The worker will not be able to access the HTML page.
var i = 0;
function timedCount() {
i = i + 1;
postMessage({
t: 'number',
i: '<b>'+ i + '</b>',
ts: new Date()
});
setTimeout("timedCount()", 1000);
}
function testAjax() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (e) {
if (xhr.readyState == 4 && xhr.status == 200) {
// upon loaded succesfully, pass it to the HTML page.
postMessage({
t: 'data',
data: xhr.response
});
}
}
xhr.open('get', 'test-data.txt');
xhr.send();
}
// message from the HTML page.
self.onmessage = function (e) {
console.log('worker...', e.data)
}
timedCount();
setTimeout(testAjax, 3000)
Subscribe to:
Comments (Atom)
We are moving
We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm
-
Overview Usually, we start developing a new system with designing database and then writing codes. If you are not a fan of ORM, you will h...
-
Below is the sample HTML which will hide the URL at the page header when printing with Javascript. <html moznomarginboxes > ...
-
You can draw a simple pie chart & doughnut chart with SVG + CSS. This is quite simple and straight forward. But the limitation is that i...