{"id":709,"date":"2023-03-24T05:00:00","date_gmt":"2023-03-24T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=709"},"modified":"2023-03-23T15:51:24","modified_gmt":"2023-03-23T22:51:24","slug":"rxjs-share-and-sharereplay-operators","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-share-and-sharereplay-operators\/","title":{"rendered":"RxJs share and shareReplay operators"},"content":{"rendered":"\n<p>Last week, we covered the difference between <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/hot-and-cold-rxjs-observables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">hot and cold Observables<\/a> in RxJs.<\/p>\n\n\n\n<p>Today, let&#8217;s talk about how we can make a cold Observable hot. Why would we want that? Say our Observable is an HTTP request we want to trigger before any component subscribes to it.  We saw that Observables from the <code>HttpClient<\/code> are cold, meaning that they only start fetching data when a subscriber comes over.<\/p>\n\n\n\n<p>By making the Observable hot, the HTTP request would get triggered immediately. The <code><a href=\"https:\/\/rxjs.dev\/api\/operators\/share\" target=\"_blank\" rel=\"noopener\" title=\"\">share<\/a><\/code> operator would do exactly that for us:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"430\" height=\"71\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-38.png\" alt=\"\" class=\"wp-image-710\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-38.png 430w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-38-300x50.png 300w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Under the hood, share turns our Observable into a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-subjects-when-and-why\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Subject<\/a>, which can handle multiple subscribers and is hot by default.<\/p>\n\n\n\n<p>The only problem with the above example is that the data could be emitted before any subscriber gets the chance to subscribe to that new hot Observable.<\/p>\n\n\n\n<p>As a result, it makes more sense to use <code><a href=\"https:\/\/rxjs.dev\/api\/operators\/shareReplay\" target=\"_blank\" rel=\"noopener\" title=\"\">shareReplay<\/a><\/code> instead as it&#8217;s going to replay the latest value(s) to any new subscriber (just like a <code>ReplaySubject<\/code>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"79\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-39.png\" alt=\"\" class=\"wp-image-711\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-39.png 450w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-39-300x53.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This code is much better because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The request is going to fire instantly before any subscription happens<\/li>\n\n\n\n<li>Any future subscriber will receive the same data from that original request instead of firing a new request.<\/li>\n<\/ul>\n\n\n\n<p>You can look at this <a href=\"https:\/\/blog.angulartraining.com\/how-to-cache-the-result-of-an-http-request-with-angular-f9aebd33ab3?sk=30e048f649e7b53e69fd71be66f0586f\" target=\"_blank\" rel=\"noopener\" title=\"\">blog post for an illustration of the above operators with a code example<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week, we covered the difference between hot and cold Observables in RxJs. Today, let&#8217;s talk about how we can make a cold Observable hot. Why would we want that? Say our Observable is an HTTP request we want to trigger before any component subscribes to it. We saw that Observables from the HttpClient are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,10],"tags":[],"class_list":["post-709","post","type-post","status-publish","format-standard","hentry","category-operators","category-rxjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=709"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/709\/revisions"}],"predecessor-version":[{"id":713,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/709\/revisions\/713"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}