{"id":6720,"date":"2019-02-03T13:40:10","date_gmt":"2019-02-03T13:40:10","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6720"},"modified":"2019-02-03T13:44:18","modified_gmt":"2019-02-03T13:44:18","slug":"using-openfin-interapplicationbus","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-openfin-interapplicationbus\/","title":{"rendered":"Using Openfin InterApplicationBus"},"content":{"rendered":"<p>In a previous post <a href=\"http:\/\/putridparrot.com\/blog\/remoting-using-wcf\/\" rel=\"noopener\" target=\"_blank\">(Remoting using WCF)<\/a> I covered interprocess communications using remoting and named pipes. If you&#8217;re using <a href=\"https:\/\/openfin.co\/\" rel=\"noopener\" target=\"_blank\">Openfin<\/a> there&#8217;s an InterApplicationBus which basically allows for the same interprocess communication\/messaging functionality.<\/p>\n<p>Let&#8217;s see how this works. Let us assume we&#8217;re using Openfin and we have one application which should send a message and a second application which will receive a message. <\/p>\n<p><strong>Let&#8217;s see how to write some code that responds to messages<\/strong><\/p>\n<p>We&#8217;ll start by creating a WPF\/Winforms or other application that remains running until explicitly closed. We want the application to remain open and wait listening for messages. <\/p>\n<p>Add the OpenfinDesktop NuGet package (Install-Package OpenfinDesktop) to your application. Now within the class of your main window\/form place the following <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Openfin.Desktop;\r\n\/\/ may need this \r\n\/\/ using Window = System.Windows.Window;\r\n<\/pre>\n<p>we&#8217;ll make it simple by writing all our code within the MainWindow.xaml.cs constructor (for our WPF app.). So add the following to the constructor after InitializeComponent();<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar runtime = Runtime.GetRuntimeInstance(new RuntimeOptions {Version = &quot;9.61.38.40&quot; });\r\nruntime.Connect(() =&gt;\r\n{\r\n   runtime.InterApplicationBus.subscribe(&quot;my-topic&quot;, (uuid, topic, message) =&gt;\r\n   {\r\n      if (message != null)\r\n      {\r\n         Debug.WriteLine(message.ToString());\r\n      }\r\n   });\r\n});\r\n<\/pre>\n<p>The Version used is important as it relates to code that is either already available on your machine or it should download, see <a href=\"https:\/\/developer.openfin.co\/versions\/?product=Runtime\" rel=\"noopener\" target=\"_blank\">OpenFin Versions<\/a>. In my case this downloaded the latest code and hence when I ran the application I got an Openfin popup showing the download progress. The runtime etc. will be stored on your local storage (by default) in %localappdata%\\OpenFin.<\/p>\n<p>Also be aware &#8211; as you&#8217;d probably assume, the InterApplicationBus is running on it&#8217;s own thread, hence when you are handling messages it&#8217;s your responsibility to handle any marshalling, for example onto the GUI\/Main thread.<\/p>\n<p>We can also handle messages in a more event style for example, replace the code within the Connect method (above) with the following (this code also rids us of the lowe cased <em>subscribe<\/em> method name which is not in keeping with the C# naming conventions :-), beyond that I&#8217;m not wholly sure at this time if there&#8217;s any other difference in this code).<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nInterApplicationBus.Subscription&lt;string&gt;(runtime, &quot;my-topic&quot;).MessageReceived += (sender, msg) =&gt;\r\n{\r\n   if (msg != null)\r\n   {\r\n      Debug.WriteLine(msg.Message);\r\n   }\r\n};\r\n<\/pre>\n<p><strong>Let&#8217;s see how to write some code that publishes messages<\/strong><\/p>\n<p>Create an second application, again make it a WPF\/WinForms application so we don&#8217;t have to write code to keep the application open whilst we connect and send messages as these are asynchronous in nature.<\/p>\n<p>Again, add the OpenfinDesktop NuGet package and using statements but within the constructor we&#8217;ll write the following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar runtime = Runtime.GetRuntimeInstance(new RuntimeOptions { Version = &quot;9.61.38.40&quot; });\r\nruntime.Connect(() =&gt;\r\n{\r\n   runtime.InterApplicationBus.Publish(&quot;my-topic&quot;, &quot;Hello World&quot;);\r\n});\r\n<\/pre>\n<p>As you&#8217;d assume, this publishes a message to the bus with the topic <em>my-topic<\/em>, which our first application listens for. In this case a simple string (good old Hello World) is being sent, but we could send something a little more complex like a JSON message or the likes.<\/p>\n<p>Now simply run the first application and once connected run the second and you should see the <em>Hello World<\/em> text in your Debug window. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post (Remoting using WCF) I covered interprocess communications using remoting and named pipes. If you&#8217;re using Openfin there&#8217;s an InterApplicationBus which basically allows for the same interprocess communication\/messaging functionality. Let&#8217;s see how this works. Let us assume we&#8217;re using Openfin and we have one application which should send a message and a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[234],"tags":[],"class_list":["post-6720","post","type-post","status-publish","format-standard","hentry","category-openfin"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6720"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6720\/revisions"}],"predecessor-version":[{"id":6724,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6720\/revisions\/6724"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}