Category Archives: Openfin

Using Openfin InterApplicationBus

In a previous post (Remoting using WCF) I covered interprocess communications using remoting and named pipes. If you’re using Openfin there’s an InterApplicationBus which basically allows for the same interprocess communication/messaging functionality.

Let’s see how this works. Let us assume we’re using Openfin and we have one application which should send a message and a second application which will receive a message.

Let’s see how to write some code that responds to messages

We’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.

Add the OpenfinDesktop NuGet package (Install-Package OpenfinDesktop) to your application. Now within the class of your main window/form place the following

using Openfin.Desktop;
// may need this 
// using Window = System.Windows.Window;

we’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();

var runtime = Runtime.GetRuntimeInstance(new RuntimeOptions {Version = "9.61.38.40" });
runtime.Connect(() =>
{
   runtime.InterApplicationBus.subscribe("my-topic", (uuid, topic, message) =>
   {
      if (message != null)
      {
         Debug.WriteLine(message.ToString());
      }
   });
});

The Version used is important as it relates to code that is either already available on your machine or it should download, see OpenFin Versions. 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.

Also be aware – as you’d probably assume, the InterApplicationBus is running on it’s own thread, hence when you are handling messages it’s your responsibility to handle any marshalling, for example onto the GUI/Main thread.

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 subscribe method name which is not in keeping with the C# naming conventions :-), beyond that I’m not wholly sure at this time if there’s any other difference in this code).

InterApplicationBus.Subscription<string>(runtime, "my-topic").MessageReceived += (sender, msg) =>
{
   if (msg != null)
   {
      Debug.WriteLine(msg.Message);
   }
};

Let’s see how to write some code that publishes messages

Create an second application, again make it a WPF/WinForms application so we don’t have to write code to keep the application open whilst we connect and send messages as these are asynchronous in nature.

Again, add the OpenfinDesktop NuGet package and using statements but within the constructor we’ll write the following code

var runtime = Runtime.GetRuntimeInstance(new RuntimeOptions { Version = "9.61.38.40" });
runtime.Connect(() =>
{
   runtime.InterApplicationBus.Publish("my-topic", "Hello World");
});

As you’d assume, this publishes a message to the bus with the topic my-topic, 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.

Now simply run the first application and once connected run the second and you should see the Hello World text in your Debug window.