Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
How to keep shareReplay alive when there are no subscribers?

I need to share a hot Observable between multiple subscribers and emit the latest value to new subscribers. I am trying to achieve this with shareReplay(1), however, the first subscriber fails to retrieve the latest value because the shareReplay operator doesn't do anything when there is no subscription, so it doesn't store anything for the first subscriber.

How do I fix this without having a permanent subscription just to keep shareReplay alive?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can solve your problem using publishReplay(1) instead of shareReplay(1):

const subject = new Subject()

const source$ = subject.pipe(publishReplay(1))
source$.connect();

subject.next(1)
subject.next(2)
subject.next(3)
source$.subscribe(v => console.log(v))
subject.next(4)
subject.next(5)
subject.next(6)

Output:

3
4
5
6

The important part is that with publishReplay, you can control when the subscription starts (source$.connect()). shareReplay will start only with the first subscription.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!