Search This Blog

Saturday, July 10, 2010

Silverlight: Comunication between Silverlight Applications with Typed Messages

Summary: The article shows how to use Eneter.Messaging.Framework for the communication between Silverlight applications to send typed messages (no size limitation).



Silverlight is a nice technology with many useful features to develop cool web-applications. But if you want to communicate between Silverlight applications the possibilities are very limited. Silverlight supports only string messages with limited size (max 40 kilobytes long). If the communication scenario is very simple this basic possibilities can be sufficient. But if your communication needs to recognize between different messages, or you need to communicate request-response with different message types, or you just need messages bigger than 40 KB then the basic Silverlight messaging is not sufficient and you must implement the whole communication "plumbing" in yourself.

Eneter.Messaging.Framework provides a lot of possibilities for the communication between Silverlight applications. For the beginning I would like to show how to use the framework to overcome the known limitations of Silverlight.

The example bellow shows how to send messages of desired type from one silverlight application to another.

1. Add Eneter.Messaging.Framework.Silverlight.dll into your Silverlight applications references to use the messaging functionality. (Full, not limited and for non-commercial usage free version of the framework can be downloaded from www.eneter.net.)

2. Implement Silverlight application receiving typed messages.
The code with using the framework is very simple:

using System.Collections.ObjectModel;
using System.Windows.Controls;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.SilverlightMessagingSystem;

namespace StrongTypeReceiver
{
    public partial class MainPage : UserControl
    {
        // Data type to be sent as the message.
        public class BasketItem
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public int Price { get; set; }
        }

        public MainPage()
        {
            InitializeComponent();

            // Initialize your UI.
            BasketItems.ItemsSource = myBasketItems;

            // Create input channel to receive message from Silverlight.
            // Note: The channel id (in our case BasketId) represents the address of the receiver.
            IMessagingSystemFactory aMessagingFactory = new SilverlightMessagingSystemFactory();
            IInputChannel anInputChannel = aMessagingFactory.CreateInputChannel("BasketId");

            // Instantiate strong typed receiver.
            ITypedMessagesFactory aTypedMessagesFactory = new TypedMessagesFactory();
            myTypedMessageReceiver = aTypedMessagesFactory.CreateTypedMessageReceiver<BasketItem>();
            
            // Handler for received messages.
            myTypedMessageReceiver.MessageReceived += OnTypedMessageReceived;

            // Attach input channel and listen.
            myTypedMessageReceiver.AttachInputChannel(anInputChannel);
        }

        // Processes the recived message - the message is of type BasketItem.
        private void OnTypedMessageReceived(object sender, TypedMessageReceivedEventArgs<BasketItem> e)
        {
            // Store received data into a collection observed by your UI.
            myBasketItems.Add(e.MessageData);
        }


        private ObservableCollection<BasketItem> myBasketItems = new ObservableCollection<BasketItem>();

        //Strong typed message receiver.
        private ITypedMessageReceiver<BasketItem> myTypedMessageReceiver;
    }
}

3. Implement Silverlight application sending typed messages.
The sender side (client) is very simple too.

using System.Windows;
using System.Windows.Controls;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.SilverlightMessagingSystem;

namespace StrongTypeSender
{
    public partial class MainPage : UserControl
    {
        // Data structure to be sent.
        public class BasketItem
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public int Price { get; set; }
        }

        public MainPage()
        {
            InitializeComponent();

            // Create output channel to send message via Silverlight.
            // Note: Use the same channel id (address) as receiver.
            IMessagingSystemFactory aMessagingFactory = new SilverlightMessagingSystemFactory();
            IOutputChannel anOutputChannel = aMessagingFactory.CreateOutputChannel("BasketId");

            // Instantiate strong typed sender.
            ITypedMessagesFactory aTypedMessagesFactory = new TypedMessagesFactory();
            myTypedMessageSender = aTypedMessagesFactory.CreateTypedMessageSender<BasketItem>();
            myTypedMessageSender.AttachOutputChannel(anOutputChannel);
        }

        private void SendButton_Click(object sender, RoutedEventArgs e)
        {
            BasketItem aBasketItem = new BasketItem();
            aBasketItem.Id = IdTextBox.Text;
            aBasketItem.Name = NameTextBox.Text;
            
            int aPrice;
            if (int.TryParse(PriceTextBox.Text, out aPrice))
            {
                aBasketItem.Price = aPrice;

                // Send the the message.
                myTypedMessageSender.SendMessage(aBasketItem);
            }
        }


        // Strong typed message sender.
        private ITypedMessageSender<BasketItem> myTypedMessageSender;
    }
}

This is really all what you need if you use the framework.

Eneter.Messaging.Framework has much more capabilities as described above. It offers also request-response communication, well defined components (e.g Broker for publish-subscribe communication), ... .
The same framework can be used for communication between the Silverlight application and the hosting server. And the same framework can be also used to implement the message based communication between threads inside the Silverlight application.
I will write more articles about the communication based on this platform.

Would you like to know more technical details about this platform, you can read here:  http://www.eneter.net/OnlineHelp/EneterMessagingFramework/Index.html

No comments:

Post a Comment