(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Communication between Silverlight applications on the client side

(8 votes)
Emil Stoychev
>
Emil Stoychev
Joined Oct 15, 2007
Articles:   23
Comments:   98
More Articles
11 comments   /   posted on Mar 24, 2009
Categories:   Patterns and Practices , General

This article is compatible with the latest version of Silverlight.

A completely new feature in Silverlight is the ability to establish a local connection between loaded Silverlight applications. In this context connection means exchanging messages and it happens only on the client side – no server roundtrip occurs.

This is useful mainly in scenarios where you need a communication channel between multiple applications hosted on single page (like navigation and other controls) and integration between out-of-browser applications. The connection is highly configurable and is not limited only to applications living only on one page or domain.

Let’s start with a demo which demonstrates a communication between two Silverlight applications hosted on a single page.

Navigation

Get Microsoft Silverlight

Content

Get Microsoft Silverlight

Download source code

In general what we need is a sender application and a receiver application. In the demo above Navigation is the sender, Content is the receiver.

Sender - LocalMessageSender

LocalMessageSender class is defined in System.Windows.Messaging namespace and it is used to send message to local listeners.

In order to send a message we need the receiver’s name and optionally the receiver’s domain.  We can send the message either to a specific domain(pass domain name as receiverDomain) or without specifying a domain scope(pass System.Windows.Messaging.LocalMessageSender.Global).

LocalMessageSender sender = new LocalMessageSender( 
    "Receiver Name", 
    System.Windows.Messaging.LocalMessageSender.Global );            
 
sender.SendCompleted += ( object sender, SendCompletedEventArgs e ) =>
{
    ...
};
sender.SendAsync( "Message" );

Using SendAsync method we send the message. After the message is sent the SendCompleted event is fired. If the method didn’t succeed a SendFailedException is thrown which can be obtained from the SendCompletedEventArgs.

Receiver – LocalMessageReceiver

The receiver is just as simple as the sender. You can specify a list of allowed sender domains, i.e. from which domains you want to receive messages.

List<string> allowedSenderDomains = new List<string>();
allowedSenderDomains.Add( "silverlightshow.net" );
allowedSenderDomains.Add( "www.silverlightshow.net" );
allowedSenderDomains.Add( "localhost" );
 
LocalMessageReceiver receiver = new LocalMessageReceiver( 
    "Receiver Name",
    ReceiverNameScope.Global, 
    allowedSenderDomains  );
receiver.MessageReceived += ( object sender, MessageReceivedEventArgs e ) =>
{
    ...
};
receiver.Listen();

Using Listen we start waiting for a message. MessageReceived event is fired once a message, sent to “Receiver Name”, is received. In MessageReceivedEventArgs there is a property Response which you can use to immediately send a response to the sender.


Subscribe

Comments

  • -_-

    RE: Communication between Silverlight applications on the client side


    posted by Pete on Mar 25, 2009 17:35
    Can you use this mechanism to communicate between a Silverlight app and a WPF (or Winform) desktop app?
  • emil

    RE: Communication between Silverlight applications on the client side


    posted by emil on Mar 26, 2009 05:19
    @Pete - there is no API available for WPF and WinForms applications that can enable such communication. What scenario do you have? Why do you need such communication?
  • -_-

    RE: Communication between Silverlight applications on the client side


    posted by mark on Mar 30, 2009 15:01
    This type of mechanism (silverlight to installed client) would enable sites that use activeX controls to go cross browser. 
  • -_-

    RE: Communication between Silverlight applications on the client side


    posted by MikeH on Mar 31, 2009 17:03
    Nice sample.
     
    I  think it is very important to enable communication with local wpf applications. Right now it is not possible to use webcams and microphones. Using local connections this would be possible, also the integration with local apps could be far better. Even Drag&Drop with local applications would be possible.
  • -_-

    nasty HRESULT E_FAIL error, no multicast send to instances of a group of windows


    posted by Dan Vanderboom on Apr 29, 2009 09:56
    Be careful that you check for exceptions when calling Listen, and call Dispose in case you do, otherwise your listening name will be unavailable for future runs of the app.  I found this because I tried setting a name of "Everyone" and having multiple instances of the app try to set up listeners with that same name.  The idea was that I would multicast send to Everyone from any of the instances, but names must be unique and I don't see a way to send to any instance of a group of browser windows... although I've devised a workaround pattern to do so, it's not nice and simple as calling SendAsync.
  • emil

    RE: Communication between Silverlight applications on the client side


    posted by emil on Apr 30, 2009 01:06
    Thanks, Dan. I think that "send to all from a given domain" feature should be addressed in the final release.
  • -_-

    RE: Communication between Silverlight applications on the client side


    posted by Uma Nagarajan on May 03, 2009 02:26

    Dan,

    I would like to know how you worked around this HRESULT_E_FAIL error and have a message sent to multiple instances of the window. Please post a sample if you have one. Thanks!

  • -_-

    RE: Communication between Silverlight applications on the client side


    posted by Adam on May 05, 2009 06:38
    Hi,

    you can find full source code and explanation on Dan's web site

    http://dvanderboom.wordpress.com/2009/04/29/multicasting-with-silverlight-3-local-messaging/

    hope it helps :)

  • -_-

    RE: Communication between Silverlight applications on the client side


    posted by ALI on Dec 08, 2009 12:25

    Hi

    I echo Emil's comment - the send to all option in a specific domain should be an integral feature of the functionality.

  • -_-

    RE: Communication between Silverlight applications on the client side


    posted by youtubeline on Dec 27, 2009 00:33
    you can find full source code and explanation on Dan's web site
  • lnikolov

    RE: Communication between Silverlight applications on the client side


    posted by lnikolov on Feb 14, 2011 17:21
    The article has been updated to the latest version of Silverlight and Visual Studio.

Add Comment

Login to comment:
  *      *