{"id":5264,"date":"2024-10-14T13:44:16","date_gmt":"2024-10-14T13:44:16","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5264"},"modified":"2024-10-15T04:36:49","modified_gmt":"2024-10-15T04:36:49","slug":"reactive-programming-io-models","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/reactive-programming-io-models\/","title":{"rendered":"Reactive Programming IO Models"},"content":{"rendered":"<p><strong class=\"ctaHeader3\">Communication Models<\/strong><\/p>\n<ol>\n<li>Synchronous + Blocking &#8211; Calling Customer Service and waiting for response online<\/li>\n<li>Asynchronous- Asking My Friend to Call Customer Service and I carry forward with my work<\/li>\n<li>Non-Blocking- Asking Call back from Customer Service and I carry forward with my work<\/li>\n<li>Asynchronous + Non Blocking &#8211; I am Calling customer Service and asking to call back My Friend and I carry forward with my work<\/li>\n<\/ol>\n<p>request -> response<br \/>\nrequest -> streaming response (Stock Price in Stock Market App, Heart Beat for Health Check in Spring Boot Appp)<br \/>\nstreaming request -> response (Using Google Docs and updating in drive in regular time intervals)<br \/>\nstreaming request -> streaming response (Playing Game Online)<\/p>\n<p><strong  class=\"ctaHeader2\">Reactive Stream Specification<\/strong><br \/>\nProcess Stream of Messages in a Non Blocking Asynchronous manner with Observer Design Pattern(Observe and React incase of change) <\/p>\n<ol>\n<li><strong>Publisher:<\/strong> Emits a sequence of elements to its subscribers.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvoid subscribe(Subscriber&lt;? super T&gt; s)\r\n<\/pre>\n<\/li>\n<li><strong>Subscriber:<\/strong> Consumes elements provided by a Publisher.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvoid onSubscribe(Subscription s)\r\nvoid onNext(T t)\r\nvoid onError(Throwable t)\r\nvoid onComplete()\r\n<\/pre>\n<\/li>\n<li><strong>Subscription:<\/strong> Represents a one-to-one lifecycle of a Subscriber subscribing to a Publisher.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvoid request(long n)\r\nvoid cancel()\r\n<\/pre>\n<\/li>\n<li>Processor: Represents a processing stage, which is both a Subscriber and a Publisher.Inherits both Subscriber and Publisher interfaces.<\/li>\n<\/ol>\n<p>There would be one Publisher at Top, similar to root of tree and there would be 0 to N intermediate processors(subscriber + publisher) and there would be leaf Subscriber<br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/10\/publisher-subscriber-model.jpg\" alt=\"\" \/><\/p>\n<p><strong  class=\"ctaHeader2\">Publisher, Subscriber and Subscription<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Publisher&lt;T&gt; {\r\n   public void subscribe(Subscriber&lt;? super T&gt; s);\r\n}\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Subscription {\r\n   public void request(long n);\r\n   public void cancel();\r\n}\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Subscriber&lt;T&gt; {\r\n   public void onSubscribe(Subscription s);\r\n   public void onNext(T t);\r\n   public void onError(Throwable t);\r\n   public void onComplete();\r\n}\r\n<\/pre>\n<ol>\n<li>Publisher will have subscribe method through which we would pass the subscriber instance. The Subscription object would be returned by Publisher <\/li>\n<li>The Publisher hands over subscription object to Subscriber. Subscriber uses onSubscribe method to accept subscription. <\/li>\n<li>Subscriber could use subscription object using request method and could cancel subscription. Communication between Publisher and Subscriber happens using subscription object<\/li>\n<li>Subscriber can request N items using subscription object. The Publisher can iterate to N object using onNext method. Publisher only give 3 items if 3 items is requested by subscriber.<\/li>\n<li>If the Publisher has completed transferring all Items, then Publisher can call onComplete() method in Subscriber to notify the subscriber that its work is done<\/li>\n<li>Publisher calls onError() method to notify error.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/10\/subpub.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/10\/ReactiveFlow-1.jpg\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Communication Models Synchronous + Blocking &#8211; Calling Customer Service and waiting for response online Asynchronous- Asking My Friend to Call Customer Service and I carry forward with my work Non-Blocking- Asking Call back from Customer Service and I carry forward with my work Asynchronous + Non Blocking &#8211; I am Calling customer Service and asking&hellip; <a href=\"https:\/\/codethataint.com\/blog\/reactive-programming-io-models\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369],"tags":[],"class_list":["post-5264","post","type-post","status-publish","format-standard","hentry","category-reactive-programming"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5264"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5264\/revisions"}],"predecessor-version":[{"id":5278,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5264\/revisions\/5278"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}