Duplex Communication (Pull + Push)

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:

  • Sockets
  • HTTP Polling Duplex
  • net.tcp (a higher-level sockets implementation)

Each of these technologies has their own advantages and disadvantages, described as follows:

  • Sockets: As sockets are a low-level means of communicating over a network, they require a lot more effort to implement than higher-level technologies. Unless you need such low-level control, you are usually better off using a higher-level technology like NET.TCP. Support for sockets in Silverlight can be found in the 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.
  • HTTP Polling Duplex: This is a long polling mechanism that is a part of WCF. A connection is established with the server, and it polls the server for messages over this connection. One of the key benefits of HTTP polling duplex is that it communicates over port 80/443 (the HTTP/HTTPS ports), which firewalls do not typically block. It also supports encryption of the data when using SSL. However, its main disadvantage is its lack of performance and reduced scalability in comparison to net.tcp.
  • net.tcp: This is a higher-level, sockets-type communication mechanism that is a part of WCF. It has much better performance over HTTP polling duplex, and is also much more scalable. The downside is that, like sockets, you are restricted to only using ports 4502 to 4534 unless you are running with elevated privileges, and traffic over this connection 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.

images 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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.120.206