Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RxJS in Action: RxJS and Webworkers?  RSS feed

 
Stephan Wissel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used RxJS (mostly copy and past, I confess) with ng/rx and I loved it.
Looking at the current PWA fashion it seems that state of the art is to have all network communication done in a webworker.
How does that work in RxJS? Can an Observable be shared across the boundary between webworker and main thread?
Is there some sample?
 
Pau Daniels
Author
Greenhorn
Posts: 8
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good question! It is something I entertained as well when I first learned about Web Workers, since they seem very conceptually related to threads.

The Web Worker interface however I think would make it difficult for this type of integration, since it runs with its own script context and to my knowledge it only allows the transfer of strict json or Transferrable, whereas with a Scheduler we would need to also pass arbitrary tasks to be executed. So while we tend to think of WebWorkers as multi-threading for JavaScript, I think conceptually they are closer to a light-weight process type instead.

That's not to say we can't use Web Workers to perform some heavy lifting, we would just use a different mechanism. Here is an admittedly toy example:



In the above we create what I like to call a "wrinkle in space and time" to create a Subject appears to be mapping inputs to outputs with as we normally see with our standard Subject, however in this case the inbound message is actually going to the Web Worker and the outbound message is the result sent back from the Web Worker, making it transparently inline with your stream.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!