I came to say this as well. Surely it is the proper way to do this. Could be combined with weblocks if you want some master/leader event bus tab or web worker
The subscribe() implementation seems suboptimal in the case where there are many different topics:
subscribe(topic, callback) {
if (this.listeners[topic] == undefined) {
// Not yet any listener for this topic
this.listeners[topic] = [];
window.addEventListener('storage', (e) => {
const dataKey = topic + "_data";
if (e.key === dataKey) {
const data = JSON.parse(e.newValue)[0];
this.listeners[topic].forEach((v, k) => {
v(data);
});
}
}, false);
}
this.listeners[topic].push(callback)
}
This installs a handler for every single topic, and every time a message is published, the handlers for all topics are called, even though at most one is interested in the change. A more efficient implementation would install a single handler, e.g. (untested):
I built a ComfyUi node that let you run a little paint program in another tab. (Sorry about the lack of updates if you use it. I intend to get back to it)
Negotiating the communication between tabs was by far the hardest part. In the end I ended up using local storage for signaling to establish a dedicated messsageport channel.
It was such a fight to make something that re-established the connection when either page reloaded.
There are still some connection issues that I haven't been able to resolve. It seems some browsers on some systems reject messages between tabs if they were loaded hours apart.
It might be worth benchmarking a pure local storage fallback, but I'm guessing it would suffer with high traffic.
A generalised implementation that allowed switching multiple paint programs and multiple ComfyUi pages would be ideal. A PubSub might be the way to go.
There's also the issue of other parts of the app also using local storage. Need to not step on toes.
I remember implementing the same thing in our framework like a decade ago, but eventually taking it out. We wanted to use it for caching data between tabs, so as not to hit the server again per tab:
You can use a SharedWorker now instead of picking a tab to do that work (and failing over to another tab if it gets closed). Support is still spotty though.
The audio demo makes me think of Bandcamp which will pause music that you're playing if another Bandcamp tab starts playing a song separately. Must be a similar mechanism under the hood
> The Web Locks API allows scripts running in one tab or worker to asynchronously acquire a lock, hold it while work is performed, then release it. While held, no other script executing in the same origin can acquire the same lock, *which allows a web app running in multiple tabs or workers to coordinate work and the use of resources.*
I did something similar (local storage events) in time of jQuery to sync shopping cart between tabs. Very simple solution, but, IIRC, it didn't work with IE.
I think in their case, offline is as in you don't need to set up a pubsub server and the client doesn't have to talk to a server for the specific pubsub functionality, not as in "use this for offline web pages/html files locally" (it may or may not work for that, I have no idea, didn't look).
It means, that you don't need an internet connection for this to work :) (so it is no rabbitmq or so which runs on a server and the browser is just the client)
You can try on the demopage when you
1. play the songs each (for them to buffer a little audio snippet)
Which is a good thing :) I am all in for asking questions. If someone can't answer them, it usually means they have not thought about it and are either ignorant or will learn something themselves :D
(like me for your question, as I never tried TabSub with internet disconnected. Was delighted to see it works nevertheless)
"easiest/low tech" would be some sort of unix file-locking and inode. Only works if you are on the same host machine though...AAAND sounds like it would be quite hacky.
https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_C...
reply