admin管理员组文章数量:1316023
How does one share a single WebSocket connection across multiple browser tabs?
I'm aware of multiple questions/posts suggesting SharedWorker. However, it is not a viable solution due to the poor adoption of SharedWorker (only 35% globally according to Can I use at the time of writing), not to mention it has been dropped in Safari.
I have implemented a working version using SerivceWorker, which has some SharedWorker's capability, to share a single connection across tabs. Unfortunately during testing I realized that ServiceWorker can be killed/stopped by the browser, especially when the devtool is not opened. I'm also aware of some workaround to prevent this behaviour, such as pinging the ServiceWorker periodically. I would like to ask whether there is a proper way to handle this.
How does one share a single WebSocket connection across multiple browser tabs?
I'm aware of multiple questions/posts suggesting SharedWorker. However, it is not a viable solution due to the poor adoption of SharedWorker (only 35% globally according to Can I use at the time of writing), not to mention it has been dropped in Safari.
I have implemented a working version using SerivceWorker, which has some SharedWorker's capability, to share a single connection across tabs. Unfortunately during testing I realized that ServiceWorker can be killed/stopped by the browser, especially when the devtool is not opened. I'm also aware of some workaround to prevent this behaviour, such as pinging the ServiceWorker periodically. I would like to ask whether there is a proper way to handle this.
Share Improve this question asked May 2, 2022 at 2:30 Alexander QiuAlexander Qiu 1172 silver badges8 bronze badges 2- 1 Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented May 2, 2022 at 3:51
- 1 Sharing websocket across browser tabs? may help – Eskandar Abedini Commented Jul 1, 2022 at 13:44
1 Answer
Reset to default 7 +200Unfortunately is not a straightforward task. But because many browsers are limiting the number of connections to the same origin
it is an issue I faced a few times.
Sharing a WebSocket
Assuming you are forced to share the connection and other options are not feasible, these are the possibilities:
- SharedWorker (33.17% support). Allows you to share the connection as you already discovered.
- BroadcastChannel (91.23% support). Allows you to municate between tabs.
- SessionStorage (97.25%). Allows you to municate between tabs. This is a bit of a hack see but worked well for me.
The trick for 2 and 3 is that you will have only one tab holding the WS connection and sharing its usage with the other tabs through the munication channel. The trickiest part is when the "main" tab is closed; you will need to elect another tab to open the connection.
Please note that both 2 and 3 are subject to buffering delays and additional delays during the change of the "main" tab. SharedWorker also has its pitfalls. Thus none of the solutions are suitable for high-speed real-time applications (like a game). If you need a real-time solution or just one that is easier to implement go for the following option:
Unsolicited Opinion
If you just need to work around the connection limitation, this is a cheat that works flawlessly: create a domain like *.wss.example.
that points to the same server and then trick the browser by randomly generating the domain on each tab (something like 3af893.wss.example.
, a6a589.wss.example.
, ...). With this pattern, you can make unlimited connections. Just remember to setup solid CORS
policies as this pattern exposes you to huge amounts of malicious traffic otherwise.
Despite implementing many of the above mentioned solutions, I ended using this one in production as it prooved to be more stable and reliable. This solution also works for Server Side Events
(SSE
).
Edit 1
Many of the mentioned options are discussed here Sharing websocket across browser tabs? as mentioned by Eskandar
本文标签: javascriptBestproper way to share a single WebSocket connection across multiple tabsStack Overflow
版权声明:本文标题:javascript - Bestproper way to share a single WebSocket connection across multiple tabs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741988540a2408831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论