Does it make sense to use WebSocket in WebWorker?

My site connects to a real-time data stream using web sockets. A data stream is just a series of JSON messages. In websocket handlers, when I get a message, I parse JSON and add some data points to the chart.

My question is: does it make sense to move the websocket to my own workflow?

At first I thought I could parse JSON in my thread and send the UI thread to a deserialized object, which could save some time. Unfortunately, postMessage seems to require me to send strings. Therefore, there is no use in parsing JSON in its thread.

It also doesn’t seem that there will be any benefit in receiving web socket data in its own stream - I would conclude that the browser is already receiving data from the wires in its stream and passes my javascript callback at the appropriate time.

So, given the fact that there is no further processing when receiving real-time data, this is mainly straight to the user interface. Does it make sense to connect to a website as a web user?

Thanks! Andrew

+5
source share
1 answer

The case when I needed to put the websocket processing in a web worker was that I needed to avoid interruptions in the browser stream in sensitive periods (when streaming audio using the web audio API that comes from my nodejs user node). Each time a web site on the browser side receives an audio data message, it interrupts the processing of the browser, which is great if your application does not have such extended periods of time. By putting websocket control into a web executor, I avoided breaking the web audio API event loop. The web manager will process the incoming data of the web socket, which it places in the circular queue of the web worker side. End of Event The browser’s web audio visualization API will be involved in this managed queue of the web manager during its own part of the cycle time of the cycle, which will avoid interruptions in the cycle of events.

See the corresponding repo https://github.com/scottstensland/websockets-streaming-audio

+2
source

All Articles