admin管理员组文章数量:1302428
My website makes a connection to a stream of realtime data using web sockets. The stream of data is just a series of JSON messages. In the websocket handlers, when I receive a message, I parse the JSON and add some data points to a graph.
My question is: does it make sense to move the websocket onto it's own worker thread?
At first I was thinking I could parse the JSON on its own thread and send the UI thread the deserialized object which might save some time. Unfortunately it looks like postMessage requires me to send strings. Therefore there's no benefit in parsing the JSON on its own thread.
It also doesn't seem like there'd be any benefit in receiving web socket data on its own thread -- I'd imaging the browser is already receiving the data off the wire on its own thread and delivering my javascript callback at the appropriate time.
So, given the fact that there isn't any post processing being done on the real time data receive -- it's mostly straight to UI -- does it make sense to put a websocket connection on a web worker?
Thanks! Andrew
My website makes a connection to a stream of realtime data using web sockets. The stream of data is just a series of JSON messages. In the websocket handlers, when I receive a message, I parse the JSON and add some data points to a graph.
My question is: does it make sense to move the websocket onto it's own worker thread?
At first I was thinking I could parse the JSON on its own thread and send the UI thread the deserialized object which might save some time. Unfortunately it looks like postMessage requires me to send strings. Therefore there's no benefit in parsing the JSON on its own thread.
It also doesn't seem like there'd be any benefit in receiving web socket data on its own thread -- I'd imaging the browser is already receiving the data off the wire on its own thread and delivering my javascript callback at the appropriate time.
So, given the fact that there isn't any post processing being done on the real time data receive -- it's mostly straight to UI -- does it make sense to put a websocket connection on a web worker?
Thanks! Andrew
Share asked Sep 5, 2015 at 15:24 AndrewAndrew 2433 silver badges10 bronze badges 2- 1 IMHO it would only make sense if you were doing significant (CPU heavy) processing on the JSON messages before sending some condensed result back to the main thread. – Alnitak Commented Sep 5, 2015 at 15:52
- 1 This question is too broad because it really happens on how much proccessing you do on the data. Making a worker adds more overhead, but if you're doing some pression or whatever, it's good to do it in worker. – Tomáš Zato Commented Oct 23, 2015 at 18:57
1 Answer
Reset to default 9Yes, an instance where I needed to put websocket handling into a web-worker was when I needed to avoid browser thread interruptions at sensitive periods (when rendering streaming audio using Web Audio API being fed from my custom nodejs server). Every time the browser-side websocket receives a message of audio data it will interrupt browser processing with an audible krackle when rendering audio, which is fine if your app has no such extended sensitive time periods. By putting websocket management into a webworker I avoided interrupting the Web Audio API event loop. The webworker would handle the websocket ining data which it put into a webworker side circular queue. The browser side Web Audio API event loop would tap into this webworker managed queue during its own event loop down time portion, thus avoiding any interruptions to the Wed Audio API event loop.
See corresponding repo https://github./scottstensland/websockets-streaming-audio
I did this work back in 2015 yet by the looks of it Web Audio API has recently acquired new tooling to handle this krackle issue https://developers.google./web/updates/2017/12/audio-worklet
本文标签: javascriptDoes putting a WebSocket on a WebWorker make senseStack Overflow
版权声明:本文标题:javascript - Does putting a WebSocket on a WebWorker make sense? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741688197a2392569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论