1

I have two observables

clients$ is a stream of client connections

data$ is a stream that delivers data endlessly

Whenever a client connects, I subscribe to data$ and send it to the client.

I read that nesting subscriptions is a no-go in RxJS, so this approach is probably wrong.

const clients$ = createClientStream()
const data$ = Observable.interval(1000).share()

clients$.subscribe(c => {

  const s = data$.subscribe(d => c.send(d))

  c.on('disconnect', () => s.unsubscribe())

})

What is the idiomatic RxJS approach to this?

1 Answer 1

2

You could use flatMap.

const clients$ = createClientStream()
const data$ = Observable.interval(1000).share()

clients$
  .flatMap(c => {
     const disconnect$ = Rx.Observable.create (observer => {
       c.on('disconnect', () => {observer.onNext({}); observer.onCompleted();})
     })
     return data$.takeUntil(disconnect$)
  }, (c,d) => c.send(d))
Sign up to request clarification or add additional context in comments.

2 Comments

how does this integrate the disconnect?
I added the disconnect part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.