Apr 22, 2008

"Pushing" Data to a Silverlight Application

The Web Service consumption feature in Silverlight 2 Beta1, based on the WCF technology and accessible through the "Add Service Refernce" dialog, makes it really easy for a Silverlight application to request data from a remote service, or to upload some data to a remote location.

But what if you want the service to initiate the conversation? Consider these examples:
  • An Instant Messaging / Chat application. When a user types a message into your Silverlight app, you use WCF to make, for example, a SendChatMessage() call. But what about receiving messages that others type back? Sure, you could call something like ReceiveChatMessage() continuously in a loop, and it could return null if there are no messages waiting. But this is a lot of work, not to mention the severe implications this would have for the performance of your app. Wouldn't it be nice if it "just worked"?
  • A Monitoring application. A lot of people are writing applications that monitor something that can be changed at any time on the server. Maybe it's a stock ticker / charting app, or maybe it's monitoring some physical sensor... Again, you could continuously poll the server to see if anything changed, but this is inefficient.

To solve problems like this, we are trying to ship a "duplex" feature in Silverlight 2 Beta2. (There is a very good chance that the feature will make it in, but of course no guarantees, and anything I say about the design is subject to change by the time we ship). I'll first give you a quick overview and then the details:

Quick Overview

Let's say you want to build a chat application. You'll start by defining a chat message such as the following:


[DataContract]
public class ChatMessage {
[DataMember] public string messageText;
[DataMember] public int textColor;
[DataMember] public bool isSmileyFace;
}

You would then create a "duplex service" on the server-side the way you would normally create a duplex service in WCF (more info below). The last step would be to configure the WCF service with a special "Silverlight duplex binding" by making an appropriate modification to Web.config.

In Silverlight, you will not be able to do an "Add Service Reference" to this service. Instead, you will manually copy the ChatMessage class to your Silverlight project. Then, after adding a new Extension assembly to your Silverlight project as well as some sample code we'll provide, you'll be able to do something like this:


var r = new DuplexReceiver<ChatMessage>("http://example.com/myService.svc");
r.OnMessageReceived += new OnMessageReceivedEventHandler<ChatMessage>(r_OnMessageReceived);


And that's all! Whenever the server chooses to send a message, you will need to handle this event by providing the following callback method:



static void r_OnMessageReceived(object sender, OnMessageReceivedEventArgs<ChatMessage> e)
{
//Handle a chat message received from the server
//E.g.:
chatBox.Text = chatBox.Text + e.Result.messageText;
//...
}

Details

For those of you familiar with WCF: What we're actually aiming to ship in Beta2 are two things:
- A new binding for WCF on the server side
- The client-side equivalent of this binding for WCF-in-Silverlight
What is not included in the plan (for Silverlight 2, not just for Beta2), is any Service Model support (like callback contracts and the like), meaning no "Add Service Reference" support.

Normally, this would mean that you need to be a WCF guru to use this (create a channel manually, receive a WCF Message, deserialize). Fortunately, we are hoping to ship sample code for DuplexReceiver < T > , that will make simple scenarios really easy (like you saw above).

For an overview of building the server side of duplex services with WCF, a good starting point is http://msdn2.microsoft.com/en-us/library/ms731064.aspx and http://msdn2.microsoft.com/en-us/library/ms731184.aspx- but here too, we may provide some sample server-side code to make this easier.

"Under the hood", the duplex mechanism works by polling the server using HTTP requests. However, this is "smart polling": the polling request to the server will not immediately return if there are no messages, but will linger around and return only when a message actually becomes available. There is a lot more to this but I can't talk about it yet (except for referring to Clarke's Third Law :)

This post is mostly to get the basic information out there - there are a lot more details to cover, which I may do in future posts.




73 comments:

Anonymous said...

Sounds good. A couple of questions...

Could I implement the same thing now by calling a web service and sleeping on the server until there is data? Is the server going to run out of threads with all of these open requests?

Anonymous said...

Wow... this is exactly what a lot of people were waiting for. Can't wait for beta2!

Anonymous said...

This is great news.. I for one am happy with this solution (thou I have questions regarding actual implementation which I know your still ironing out so I wont ask them )

With this news for WCF based duplex calls one needs to re-evaluate the need for tcpip/sockets based solutions. Alot of the scenarios people would want to build using sockets will be answered with the new duplex WCF. hmmmm food for thought !

realparadyne said...

Interesting.. We've recently built a comms system using the same technique (ajax comet?) to use with a .net based server end (using HttpListener/http.sys so no IIS) and using XBAP as the client. The point being to work full duplex through proxies/firewalls where http port 80 could be all that's allowed. We were very curious about having a Silverlight based client but weren't sure if we could do 2 http requests through different sockets like we can with the full .net framework (one for the slow polling server->client channel and one for the periodic client->server messages). This would indicate that perhaps we will be able to do that?

Anonymous said...

Very well done. While I understand that full "OperationContext" might not make it... It's still important to then make sure that the server can "know" who the caller is, as well as recieve an event (on the server) upon the client's disconnecting.

Again, well done.

Anonymous said...

Regarding Bill's comment above... the client would timeout if it waited too long (unless you configured around it).

About the threading part... if the connection is open, you have a thread open anyway. So the answer there would likely be no different than if this was just a web application and you had a lot of people hitting it with their browser.

Anonymous said...

Thanks a lot Eugene ^_^

I am still not very sure if it really satisfies our needs, but it looks to be a very good solution to many applications.

I will be waiting for the beta 2 and give it a try.

Cheers

Thomas Holloway said...

This is very exciting. I implemented a callback service for my website that I'm building and when I went to actually implement the client I found that none of the preferred classes existed.

Not to rush, any idea on what the planned release date is for Silverlight 2 Beta 2?

Anonymous said...

So, now that b2 is minutes away from release (hopefully!), can you tell us if all this awesome duplex stuff made it in? I sure hope I don't have to wait for the RTM.

Anonymous said...

I went through the sample walkthrough provided in SL2B2 SDK documentation. But the code doesn't exactly work according to the documentation.

I posted about it here (which also includes a link to download the sample code that I put together from the walkthrough).

Colin Jack said...

I'm wondering whether you can use this functionality if you are not using WCF services?

I ask because I've been looking hard at REST recently and even with the new Advanced Web model I'm not convinced that WCF is necessarily the right grounding for REST. However being able to push data to the client would undoubtedly be useful in a lot of situations.

Thomas Holloway said...

REST is just a model for posting to web services through the URL. WCF does support REST indeed.

As for callbacks, there are very few places where this functionality even exists, that's pretty much why it's quite amazing that we have the availability in WCF. :D

I can see WCF callbacks being useful in a variety of 'live' applications. Many websites use polling to a degree to replace the necessity for having to refresh the page when new content arrives. GMail has this kind of 'live' effect whenever new e-mails come in.

Colin Jack said...

@Thomas
I wasn't really asking what REST was, though to be honest I think it's a bit bigger than just being about posting to URLs.

Really I'm interested in whether this two way communication will only be supported if you use WCF, or whether you will be able to hook into it if you choose not to use WCF. My hope is its the latter...

Anonymous said...

Are there any guidelines on server tuning for optimum scalability? I expect to have a few thousand clients connected at any given time and wondering what to expect and how to best configure my server.

Anonymous said...

Does anyone know if there's theoretical limits on the number of concurrent client connections the server can handle?

Anonymous said...

When will this easy method of using duplex contracts be available in Silverlight?

Cagdas said...

I second this question:

" @Thomas
I wasn't really asking what REST was, though to be honest I think it's a bit bigger than just being about posting to URLs.

Really I'm interested in whether this two way communication will only be supported if you use WCF, or whether you will be able to hook into it if you choose not to use WCF. My hope is its the latter..."

Anonymous said...

"Smart Polling: the polling request to the server will not immediately return if there are no messages, but will linger around and return only when a message actually becomes available", but the request still ties up a thread on the server side, is that right? Assume you have thousands or more users doing that, the server will be brought to its knees.

belrion said...

buy wow goldAsesor ProfessionalUruguayProfessionalbuy wow goldOfficeLinksNotice

Anonymous said...

As a new player , you may need some game guides or information to enhance yourself.
angels gold is one of the hardest theme for every class at the beginning . You must have a good way to manage your angels online gold.If yor are a lucky guy ,you can earn so many buy angels goldby yourself.If you need , you cancheap angels online goldat our website . Go to the related page and check the detailed information . Once you have any question , you can connect our customer service at any time .


Making Sho Online Mun is the old question : Honestly there is no fast way to make lots of Sho Mun . Sadly enough a lot of the people that all of a sudden come to with millions of Sho Online gold almost overnight probably duped . Although there are a lot of ways to make lots of Sho gold here I will tell you all of the ways that I know and what I do to buy Sho Online gold.

Anonymous said...

Once I played GuildWars, I did not know how to get strong, someone told me that you must have gw gold. He gave me some GuildWars Gold, he said that I could buy Guild Wars Gold, but I did not have money, then I played it all my spare time. From then on, I got some GuildWars money, if I did not continue to play it, I can sell cheap gw gold to anyone who want.

You must have habbo credits. He gave me some habbo gold, he said that I could buy habbo gold, but I did not have money, then I played it all my spare time. From then on, I got some habbo coins, if I did not continue to play it, I can sell cheap habbo credits to anyone who want.

Anonymous said...

Once I played hero, I did not know how to get strong, someone told me that you must have hero gold. He gave me some hero online gold, he said that I could buy hero gold, but I did not have money, then I played it all my spare time. From then on, I got some hero online money, if I did not continue to play it, I can sell hero money to anyone who want.


I did not know how to get strong, someone told me that you must have last chaos gold. He gave me some lastchaos gold, he said that I could buy last chaos gold, but I did not have money, then I played it all my spare time. From then on, I got some lastchaos money, if I did not continue to play it, I can sell cheap lastchaos gold to anyone who want.

Anonymous said...

This world is just getting crazy for the www.powerlevelingspecialists.com online games, and the most recent buzzword in the www.powerlevelingspecialists.com world of gamers and especially when it’s Massive www.powerlevelingspecialists.com Multiplayer Online Role Playing Games www.powerlevelingspecialists.com (MMORPG) is www.powerlevelingspecialists.com power leveling . Such games as www.powerlevelingspecialists.com World of Warcraft , www.powerlevelingspecialists.com Warhammer online and www.powerlevelingspecialists.com Age of Conan lets you assume various roles as you www.powerlevelingspecialists.com explore adventure and quest across vast worlds. These www.powerlevelingspecialists.com games are so highly addictive some gamers have even gone as far as saying it’s similar to a Crack Cocaine addiction; just try it once and you’re hooked. These games have even been the cause of death, suicide, divorce and even bankruptcy in the most severe cases.www.powerlevelingspecialists.com/index2.php Home www.powerlevelingspecialists.com/about.php About Us www.powerlevelingspecialists.com/contact.php Contact Us www.powerlevelingspecialists.com/powerleveling.php Powerleveling www.powerlevelingspecialists.com/gold.php Buy Gold www.powerlevelingspecialists.com/reputationpl.php Reputation www.powerlevelingspecialists.com/professionpl.php Professions www.powerlevelingspecialists.com/burningcrusade.php Burning Crusade www.powerlevelingspecialists.com/purchase.php Purchase Merchandise www.powerlevelingspecialists.com/expedition.php Expeditions www.powerlevelingspecialists.com/forum Forums www.powerlevelingspecialists.com/gold.php WOW www.powerlevelingspecialists.com/gold.php WOW gold | www.powerlevelingspecialists.com/gold.php Warcraft World | www.powerlevelingspecialists.com/gold.php World of Warcraft | www.powerlevelingspecialists.com/burningcrusade.php MMORPG | www.powerlevelingspecialists.com/burningcrusade.php Role Playing Game | www.powerlevelingspecialists.com/powerleveling.php WOW Power Leveling Specialists | www.powerlevelingspecialists.com/powerleveling.php World of Warcraft Power Leveling Specialists | www.powerlevelingspecialists.com/powerleveling.php Power Leveling Specialists | www.powerlevelingspecialists.com/powerleveling.php WOW Powerleveling | www.powerlevelingspecialists.com/professionpl.php Profession powerleveling | www.powerlevelingspecialists.com/purchase.php Wow buy gold online | www.powerlevelingspecialists.com/purchase.php Wow gold for cash | www.powerlevelingspecialists.com/gold.php World of Warcraft gold | www.powerlevelingspecialists.com/powerleveling.php World of Warcraft powerleveling www.powerlevelingspecialists.com/professionpl.php World of Warcraft professions | www.powerlevelingspecialists.com/reputationpl.php Cheap powerleveling | www.powerlevelingspecialists.com/reputationpl.php reliable powerleveling | www.powerlevelingspecialists.com/powerleveling.php Powerleveling info | www.powerlevelingspecialists.com/purchase.php buy gold online | www.powerlevelingspecialists.com/expedition.php gain levels quickly | www.powerlevelingspecialists.com/expedition.php leveling warcraft world | www.powerlevelingspecialists.com/expedition.php leveling tip warcraft world | www.powerlevelingspecialists.com/reputationpl.php leveling service warcraft world | www.powerlevelingspecialists.com/reputationpl.php fast leveling warcraft world | www.powerlevelingspecialists.com/expedition.php bots leveling warcraft world www.powerlevelingspecialists.com Age of conan www.powerlevelingspecialists.com warhammering

Anonymous said...

I really liked your blog! You have some great content. Check out my blog and give me some feedback Please come visit my site Shreveport Business Directory when you got time.

Anonymous said...

Well, nice article buddy… Someone will love to read this infor if I tell her about this. She’s really interested in this subject. Thanks again… Please come visit my site Birmingham Business Directory when you got time.

Resale- Second Hand- & Used Merchandise Stores said...

As a Newbie, I am always searching online for information that can help me. Thank you. Please come visit my site Discount Stores when you got time.

Construction Equipment Rental said...

I found your site on google, great site, keep it up. Will return in the future. Submitted this post to Google News Reader. Please come visit my site Equipment Rentaland give me any valuable feedbacks.

Chicago Business Directory said...

Hello, What enticed you to post this article. It was extremely interesting, especially since I was searching for info on this subject last Thursday. Please come visit my site Chicago Yellow Page when you got time.

Phoenix Business Directory said...

I really liked your post on home financing! if you have more information elsewhere let me know. Please come visit my site Business Directory Phoenix and give me any valuable feedbacks.

Anonymous said...

Hey, you have a great blog here! With good writing and Nice writing style, Keep up the good work. Nice for share this article. This is Really nice tutorial. That's one thing I'm really looking forward. I'm definitely going to bookmark you! Looking forward to reading more from you.
Thanks for information and good day for all sad people.

------------------------------------------
Are you play online poker ? Get free starting capital and try build poker bankroll. Free Poker Sponsorship is best way to start learn poker live. Play online poker without risk. I have all possible no deposit bankrolls for new players. Texas Hold'em Poker Promotion for new members. Easy $50 bonus promo and Free poker cash bankroll.

amateur porn said...

Well, nice article buddy… Someone will love to read this infor if I tell her about this. She’s really interested in this subject. Thanks again… Please come visit my site deleted youtube videos when you got time.

business intelligence suite said...

I really liked your post on home financing! if you have more information elsewhere let me know. Please come visit my site group intelligence
when you got time.

business process management summit said...

Yeah it is a great and nice article looking forward to have such article it is so useful. Please come visit my site business technology management
give me any valuable feedbacks.

rr8004 said...

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post. Please come visit my site Sterling Silver Jewelry when you got time.

Hainan Travel said...

You share valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up. I would love some feedback on my site Hainan China when you got time.

Guilin City said...

I was thinking of looking up some of them newspaper websites, but am glad I came here instead. Although glad is not quite the right word… let me just say I needed this after the incessant chatter in the media, and am grateful to you for articulating something many of us are feeling - even from distant shores. Feel free to check out my site Guilin China when you got time.

pedro velasquez said...

If you want to build applications that sportsbook take full advantage of the new rich user interface capabilities of the .NET 3.0 Framework, you need to learn Microsoft's Windows Presentation Foundation. bet nfl Fully updated for the release of .NET 3.0, this new edition will get you up to speed quickly. The new edition includes new chapters on printing, XPS, 3-D, navigation, text and documents, along with a new appendix that covers http://www.enterbet.com Microsoft's new Silverlight platform for delivering richer UI through standard web browsers

Jewelry Store said...

You got a really useful blog I have been here reading for about an hour. I am a newbee and your success is very much an inspiration for me.I would love some feedback on my site jewelry store when you got time.

Wholesale Fashion Jewelry Gift Product said...

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog? Feel free to check out my site wholesaler when you got time.

business space security said...

You got a really useful blog I have been here reading for about an hour. I am a newbee and your success is very much an inspiration for me. Please come visit my site wireless home security system when you got time.

canada occupational safety said...

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog? Please come visit my site safety glasses give me any valuable feedbacks.

chrohns disease said...

You got a really useful blog I have been here reading for about an hour. I am a newbee and your success is very much an inspiration for me. Please come visit my site crohn disease when you got time.

symptoms of alzheimer's said...

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog? Please come visit my site alzheimer's symptoms when you got time

rr8004 said...

love your blog! Very cool! Please come visit my site Orlando Business Directory when you got time.

rr8004 said...

You owe a very nice and interesting blog. Please come visit my site Hialeah Yellow Page Business Directory when you got time.

Anonymous said...

Aftersex toysseries,asex shopof,boardadult toysdetermined,companyadult shoppast,Yahoo'ssexy lingerieweek,meetingsvibratorperson,decisionadult productsbelow,anystrap onshare,overadultshopadvantage,coulddildooffer,theMalaysia sex toysregulators,tryingSingapore sex toysdigging,massivelysex toy$31,thatCondomsaid,takeoverParadise sex toys shopbattle,woodenSex Toys Adult Shop Singapore Malaysiastand,word delivery,stand,wordParadise Sex Toys Adult Shop Singapore Malaysiadelivery,committed toParadise Sex Toys Adult Shop Singapore Malaysiacertain,schoolSex Toys Shop Singapore Malaysiaproducts,BuyParadise Sex Toys Adult Shop Singapore MalaysiaNow

Anonymous said...

sorry to ask this here but… I really love your theme, would it happen to be a free one i can download somewhere, or is it a custom theme you had made? Soon i will be launching my own blog, however i’m not great with designs but i do like the style of your site so it would be excellent if i could find (or buy) something with a similar look as my last designer cannot finish my site. Thanks! Please come visit my site Rochester Business Directory when you got time.

Anonymous said...

Congratulations, you just earned yourself an entry in my feed reader, great blog. Please come visit my site Fort Wayne Business Directory when you got time.

Anonymous said...

You share valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up. Please come visit my site Yellow Pages Of Colorado when you got time.

Anonymous said...

You share valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up. Please come visit my site Connecticut Yellow Page Business Directory when you got time.

tree service said...

I found your blog on google and read a few Thanks for the information you mentioned here, I'm looking forward to see your future posts. Cheers !! Please come visit my site landscape give me any valuable feedbacks.

floor tile said...

I am deeply in love with every single piece of information you post here. Will be back often to read more updates! Please come visit my site carpet when you got time.

Anonymous said...

This is just another reason why I like your website. I like your style of writing you tell your stories without out sending us to 5 other sites to complete the story. Please come visit my site Phone Directory Of Memphis City Tennessee TN State when you got time.

Anonymous said...

This is just another reason why I like your website. I like your style of writing you tell your stories without out sending us to 5 other sites to complete the story. Please come visit my site American Memphis online business directory when you got time.

Stivel Velasquez said...

Silverlight provides several different ways to access data stored in remote locations. Data can be pulled from Web Services and RESTful services and even pushed from servers down to clients using sockets (see my previous articles on sockets here, here and here). Silverlight 2 Beta 2 introduces another way to push data from a server to a client using Windows Communication Foundation (WCF) and HTTP. costa rica vacation rentals WCF's support for duplex service contracts makes this possible and opens up unique opportunities for pumping data to Silverlight clients. In this first part of a two part series I'll demonstrate how a WCF push service can be created and cover the steps to get a sample service up and running. The second article will focus on the client and show how to communicate with a WCF duplex service and listen for data that's sent.
http://www.meadbrown.com

Anonymous said...

What a facinating article. I’m looking for a marketing expert to help with a project, could you help? Please come visit my site San Diego Yellow Page Business Directory when you got time.

Anonymous said...

What a facinating article. I’m looking for a marketing expert to help with a project, could you help? Please come visit my site Local Business Directory Of San Diego U.S.A. when you got time.

louis said...

Thanks dude that you are creating the duplex feature in beta2 version. Can't wait for beta2!
cosmetic dentists edinburgh

rr8004 said...

I really liked your blog! Please come visit my site Business Reviews Of Denver City when you got time.

rr8004 said...

Can you provide more information on this? I am new to the subject. Please come visit my site Colorado Springs Business Phone Book when you got time.

Fashion Blogger said...

It is very interesting article and quite impressive and more informative and looking forward to read such article. Please come visit my site Fashion Blog when you got time.

Wholesale Designer Watches said...

Since I’m new to blogging, these articles are greatly appreciated; very useful and informative blog and every body must visit this blog. Please come visit my site Wholesale Watches give me any valuable feedbacks.

Getting Divorced said...

Thanks for posting such kind of usefull information.Keep Posted in future as well.Thank you! I would love some feedback on my site Divorce Advice when you got time.

Document Shredding Services said...

I just love it ..... well i don't have any doubt about your articles... your articles are awesome... Honestly you are simply the best.Thanks for sharing this with us. Please come visit my site Document Shredding when you got time.

Anonymous said...

It is very interesting article and quite impressive and more informative and looking forward to read such article. Please come visit my site BCorpus Christi Business Services And Classifieds when you got time.

furniture stores said...

I agree and enjoyed reading, I will make sure and bookmark this page and be back to follow you more.

rr8004 said...

Really great work. Congrats to everyone who are involved with this project. The website layout and graphics are really cool. Please come visit my site Colorado Springs Business Directory Forum Blog Classifieds when you got time.

rr8004 said...

Really great work. Congrats to everyone who are involved with this project. The website layout and graphics are really cool. Please come visit my site Colorado Springs Web Link when you got time.

rr8004 said...

Excellent article , i just share it with my friend of Italy. I Stumble UP your blog post , you will notice an increase of traffic within 24 hours for targeted people. Cheers . Please come visit my site Fresno Business Directory when you got time.

rr8004 said...

Excellent article , i just share it with my friend of Italy. I Stumble UP your blog post , you will notice an increase of traffic within 24 hours for targeted people. Cheers . Please come visit my site Local Business Directory Of Fresno U.S.A. when you got time.

rr8004 said...

Couldn't be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing! Please come visit my site Arizona AZ Phone Directory when you got time.

rr8004 said...

Wow, loving the two photos you posted. You got potential. Please come visit my site Raleigh Yellow Pages United States when you got time.

rr8004 said...

Wow, loving the two photos you posted. You got potential. Please come visit my site Business Yellow Pages Raleigh when you got time.