So far, we've discussed means of providing one-way ("simplex") communication with the client. The client makes a request to a service, and in return gets a response. However, sometimes you want to be able to provide two-way ("duplex") communication between the client and the server, in which the server can “push” messages to clients. The client establishes a connection with the server, which is kept alive, thus enabling the server to push messages to the client, and avoiding the need for the client to constantly poll the server for new data. This is particularly useful when you have continually changing data (such as a stock ticker), want to be able to send out notification messages to the clients, or are implementing a chat application, and you want the client to be notified of updates in “real-time."
Silverlight supports the following three technologies that enable communicating with the server in a duplex fashion:
Each of these technologies has their own advantages and disadvantages, described as follows:
System.Net.Sockets
namespace. Note that you are restricted to only using ports 4502 to 4534, unless you are running with elevated privileges. This means that sockets are best used in intranet applications, rather than public-facing applications, because the ports will possibly be blocked behind a firewall. In addition, traffic over sockets is not encrypted.As you can see, it can be a hard decision to choose between HTTP polling duplex and net.tcp. A recommended approach is to actually implement both in your application. Your application can try to establish a connection using net.tcp over one of the supported ports. If this connection fails, likely due to a firewall blocking the communication between the client and the server, your application can then fall back to HTTP polling duplex and use that instead. So you get the performance benefit of using net.tcp if it's available, but have a fallback approach with HTTP polling duplex if it's not. One issue to be aware of when using this fallback approach, however, is that you will also need to implement two different authentication schemes, assuming that the user requires authentication—one for each technology.
Note Both sockets and net.tcp require the server to have a cross-domain policy file in place in order for Silverlight applications to be able to access them. Cross-domain policy files are discussed in Chapter 5.
Discussing these means of duplex communication in depth is beyond the scope of this book, but you can find an excellent series of articles written by Gill Gleeren, covering the implementation of each of these technologies, at the following locations on the SilverlightShow web site:
www.silverlightshow.net/items/The-duplex-story-looking-at-duplex-communication-in-Silverlight-4-Part-2-Using-Sockets.aspx
www.silverlightshow.net/items/The-duplex-story-looking-at-duplex-communication-in-Silverlight-4-Part-1.aspx
www.silverlightshow.net/items/The-duplex-story-looking-at-duplex-communication-in-Silverlight-4-Part-3-Using-Net.Tcp.aspx