Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

rxjs - Is it possible/advisable to use Reactive Programming when you can't control the Publisher

I am very new to Reactive Programming using ReactiveX Libraries, even so, I have already started appreciating its fluent APIs and style of programming that avoids a class of concurrency bugs.

Therefore, I would like to use ReactiveX (specifically RxJava and RxJS) at work, where I am developing an application that essentially receives a "stream" of updates from a server and displays it to the user (after some manipulations). The way I see it, in terms of reactive-streams nomenclature, my application is a "Subscriber" that subscribes to a Publisher to receive values of interest.

I have a few doubts though that are preventing me from using RxJava at work:

  1. The "Publisher" in my case is another service (that does not use RxJava or such) that I do not control or maintain, therefore, I cannot change its code to implement "Publisher" interface. As such, is it even possible (or, at least, not un-idiomatic) for me to use RxJava? If so, would I have to create a wrapper of sorts to implement a Publisher that takes values from this other service and emits it?

  2. Is it possible/common for the Subscriber and Publisher to be implemented different languages, like the Subscriber (written using RxJS) subscribes to a Publisher written using RxJava?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The short answer

Shouldn't be a problem. Reactive programming isn't concerned with data formats or publishers at the level of a network. It's entirely possible to have only a small bit of functionality be reactive without impacting the rest of your codebase at all.

JavaScript and Java both let you switch between reactive and imperative styles with minimal fuss.

Just like it doesn't matter what language a server or a client is running so long as they can both read and send well-formed messages. Reactive libraries are generally entirely self-contained.

The longer answer

From a client's point of view, what your server is doing can be abstracted in many ways. What's simplest/best has a lot more to do with your networking libraries than the server. An angular app (for example) can treat any and all network traffic as streams using Angular's HttpClient. It doesn't matter what the server is doing, nor how it sends packets. Angular can pass them on as streams.


Even if it's not built-in, it should be relatively easy to layer reactive libraries on top of whatever else exists.

Subjects are the building block that let you seamlessly switch between reactive/functional styles and imperative styles of programming.

However you're receiving data currently, you can create a subject and use its .next to pass that data into a stream.

public payloadData: Subject;

function processData(payload){
  this.payloadData.next(payload);
}

Then you're on your way. You can build out your reactive framework from there.

/***
 * An observable, that (once subscribed) begins to keep a timestamped array of all
 * values emitted by payloadData.
 *
 * TODO: Remove stale data and write it to disk.
 ***/
function accumulateDataHistory(): Observable {
  return this.payloadData.pipe(
    timestamp(),
    scan((acc, curr) => ([...acc, curr]), []),
    shareReplay(1)
  )
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...