TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Offline JavaScript PubSub between browser tabs

83 点作者 l1am0大约 1 个月前

11 条评论

sisk大约 1 个月前
If I understand this correctly, the `BroadcastChannel` API solves a similar purpose.<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;Broadcast_Channel_API" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;Broadcast_C...</a>
评论 #43571132 未加载
评论 #43580730 未加载
sltkr大约 1 个月前
The subscribe() implementation seems suboptimal in the case where there are many different topics:<p><pre><code> subscribe(topic, callback) { if (this.listeners[topic] == undefined) { &#x2F;&#x2F; Not yet any listener for this topic this.listeners[topic] = []; window.addEventListener(&#x27;storage&#x27;, (e) =&gt; { const dataKey = topic + &quot;_data&quot;; if (e.key === dataKey) { const data = JSON.parse(e.newValue)[0]; this.listeners[topic].forEach((v, k) =&gt; { v(data); }); } }, false); } this.listeners[topic].push(callback) } </code></pre> 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):<p><pre><code> window.addEventListener(&#x27;storage&#x27;, (e) =&gt; { if (e.key.endsWith(&#x27;_data&#x27;)) { const topic = e.key.substring(0, e.key.length - 5); const data = JSON.parse(e.newValue)[0]; this.listeners[topic]?.forEach(v =&gt; v(data)); } }, false);</code></pre>
评论 #43575518 未加载
Lerc大约 1 个月前
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)<p>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.<p>It was such a fight to make something that re-established the connection when either page reloaded.<p>There are still some connection issues that I haven&#x27;t been able to resolve. It seems some browsers on some systems reject messages between tabs if they were loaded hours apart.<p>It might be worth benchmarking a pure local storage fallback, but I&#x27;m guessing it would suffer with high traffic.<p>A generalised implementation that allowed switching multiple paint programs and multiple ComfyUi pages would be ideal. A PubSub might be the way to go.<p>There&#x27;s also the issue of other parts of the app also using local storage. Need to not step on toes.
EGreg大约 1 个月前
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:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;Qbix&#x2F;Platform&#x2F;blob&#x2F;main&#x2F;platform&#x2F;plugins&#x2F;Q&#x2F;web&#x2F;js&#x2F;Q&#x2F;Frames.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Qbix&#x2F;Platform&#x2F;blob&#x2F;main&#x2F;platform&#x2F;plugins&#x2F;...</a><p>It had the concept of a “main frame” that they would route all requests to the server through.<p>I remember now — I did it not so much for tabs as for our solution of having tons of little iframes on a page: <a href="https:&#x2F;&#x2F;qbix.com&#x2F;ecosystem" rel="nofollow">https:&#x2F;&#x2F;qbix.com&#x2F;ecosystem</a>
评论 #43572059 未加载
hombre_fatal大约 1 个月前
It&#x27;s nice that the &#x27;storage&#x27; event also gives you event.newValue to spare you the race condition of reading from localStorage.
Minor49er大约 1 个月前
The audio demo makes me think of Bandcamp which will pause music that you&#x27;re playing if another Bandcamp tab starts playing a song separately. Must be a similar mechanism under the hood
评论 #43573218 未加载
kvemkon大约 1 个月前
Related recently discussed:<p>Show HN: JavaScript PubSub in 163 Bytes (31.03.2025)<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=43529774">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=43529774</a>
edweis大约 1 个月前
I love it, the code is so simple: <a href="https:&#x2F;&#x2F;github.com&#x2F;simonfrey&#x2F;tabsub&#x2F;blob&#x2F;master&#x2F;tabsub.v1.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;simonfrey&#x2F;tabsub&#x2F;blob&#x2F;master&#x2F;tabsub.v1.js</a>
评论 #43571093 未加载
评论 #43571226 未加载
nottorp大约 1 个月前
Isn&#x27;t this a security hole waiting to be exploited?<p>How does the browser handle access control to the local storage, especially offline when they aren&#x27;t loaded from the same site?<p>[Yes, I really don&#x27;t know. Yes, I&#x27;m asking. Not everyone is a web dev.]
评论 #43573560 未加载
qudat大约 1 个月前
Very cool, I love seeing pubsubs in the wild. We built an ssh based pubsub that we have been using in production for awhile now: <a href="https:&#x2F;&#x2F;pipe.pico.sh&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pipe.pico.sh&#x2F;</a>
amazingamazing大约 1 个月前
I wonder how one does this on nodejs across instances gracefully.
评论 #43575182 未加载