javascript - How to start a Server-Sent Event "only once" inside a SharedWorker to push message for any open script? -
i have server-sent event (sse) implementation working no issues. issue having "one user can have many connections server". basically, if user opens more 1 web browser's tab, each tab create brand new server-sent event request server cause many requests run single user.
to solve problem, run sse inside javascript's sharedworker.
this means have 1 sse communicating sharedworker. then, every page/web browser communication sharedworker. gives me advantage of allowing 1 sse per user.
this how sse working without type of worker.
$(function(){ //connect server read messages $(window).load(function(){ startpolling( new eventsource("poll.php") ); }); //function listen new messages server function startpolling(evtsource){ evtsource.addeventlistener("getmessagingqueue", function(e) { var data = json.parse(e.data); //handle recieved messages processserverdata(data); }, false); evtsource.onerror = function(e) { evtsource.close(); }; } });
i have same setup running. however, run inside javascript's sharedworker eliminate having more 1 sse per user.
i struggling implement sharedworker. here tried far
i created file called worker.js
, added code it
var ports = [] ; onconnect = function(event) { var port = event.ports[0]; ports.push(port); port.start(); var serv = new eventsource(icwspollingurl) serv.addeventlistener("getmessagingqueue", function(e) { var data = json.parse(e.data); processserverdata(data); }, false); }
then on page want listed messages have code
$(function(){ $(window).load(function(){ var worker = new sharedworker("worker.js"); worker.port.start(); worker.port.onmessage = function(e) { console.log(e.data); console.log('message received worker'); } }); });
what missing here?
what doing wrong?
how can correct implementation?
edited
based on comments below @bergi, here updated version of implementation still not posting messages connectors. added comments code explaining understanding of going on code.
on landing page i.e. index.php
connect sharedworker this
$(function($){ //establish connection shared worker var worker = new sharedworker("/add-ons/icws/js/worker1.js"); //listen message send worker worker.port.addeventlistener("message", function(event) { console.log(event.data); } , false ); //start connection shared worker worker.port.start(); });
this code worker1.js
file contains
var ports = [] ; //runs when new connection starts onconnect = function(event) { var port = event.ports[0]; ports.push(port); port.start(); //implement channel communication between connecter , sharedworker port.addeventlistener("message", function(event) { listenformessage(event, port); } ); } //reply message sent sharedworker same message add phrase "sharedworker said: " listenformessage = function (event, port) { port.postmessage("sharedworker said: " + event.data); } //runs every time , post message connected ports function readnewmessages(){ var serv = new eventsource(icwspollingurl) serv.addeventlistener("getmessagingqueue", function(e) { var queue = json.parse(e.data); notifyallports(queue); }, false); } //check open ports , post message each function notifyallports(msg){ for(i = 0; < ports.length; i++) { ports[i].postmessage(msg); } }
here 1 more version of worker1.js
var ports = [] ; //runs when new connection starts onconnect = function(event) { var port = event.ports[0]; ports.push(port); port.start(); //implement channel communication between connecter , sharedworker port.addeventlistener("message", function(event) { listenformessage(event, port); } ); } //reply message sent sharedworker same message add phrase "sharedworker said: " listenformessage = function (event, port) { port.postmessage("sharedworker said: " + event.data); } readnewmessages(); //runs every time , post message connected ports function readnewmessages(){ console.log('start reading...'); var serv = new eventsource(icwspollingurl); serv.addeventlistener("getmessagingqueue", function(e) { var queue = json.parse(e.data); console.log('message received'); console.log(queue); notifyallports(queue); }, false); } //check open ports , post message each function notifyallports(msg){ for(i = 0; < ports.length; i++) { ports[i].postmessage(msg); } }
Comments
Post a Comment