|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionWeb pages and ASP.NET are a wonderful tools, but as the old proverb goes, "if all you have is a hammer, everything seems like a nail". ASP.NET is a web technology, all of which exists to serve up resources to Web Browsers which request them. Thats great, and it works for the vast majority of things currently on the internet. In fact, when people think of the internet, they mostly associate it with the ubiqitous world wide web model or its HTTP protocol. But what if we want more...what if instead of Web Browsers just requesting resources, we want to push information actively out to them, without being asked first. Examples of this include; live wiki's, polls, chat, stock tickers, real-time auctions, and games. This is where the existing paradigm fails, and we have to fall back to older technologies, the roots on which the internet is founded....yes, im talking actual two-way communication, interactivity, *gasp* sockets, TCP/IP! Taking a step back, web requests can roughly be compared to a student asking a teacher questions. However, only the student is allowed to ask questions, the teacher cannot actively prompt the student with facts. This article sets out one experimental aproach to breaking that model. Most of the current solutions, collectively called "Comet", involve the Web Browser keeping a active HTTP connection to the Web Server, like a student always keeping the teachers attention. Presented here is another aproach, utilizing a "side-channel", where the "teacher" (thats the Web Server) doesnt spend as much attention or resources responding to pesky questions (web requests), and we can push information out instead, or engage in a more interactive way. BackgroundThis article is inspired and partially based on the work and code by Alex MacCaw, the developer who created the Juggernaut extension to Ruby on Rails. But why leave all the fun to those dymamic language people? Cant we have that in ASP.NET too? The answer is "yes we can." How do we communicate with the Web Browser without it making a request first? One solution is to have the Web Browser keep open a socket connection directly to the server using a small light-weight Flash component. What are the benefits of this aproach:
And yes, it does have drawbacks:
Using the CodeComet applications can be divided into two categories:
Server-pushThe work flow of using Comet in server-push scenario is:
In code with a simple "GuestBook" example:
InteractiveThe interactive scenario is very similar but instead of the Web Browser doing postbacks or AJAX calls to the Web Server, the Web Browser is using its Comet Connection to send data directly to an Application Client and vice versa. The work flow of using Comet in interactive scenario is:
The Web Browser code is still very similar, but now instead of doing AJAX calls or postbacks, we can just send data to our actively listening Application Client over the Comet connection. e.g. in response to a button click in a "sound player" application: function sounds_click(soundId)
{
comet.send (soundId);
}
The Comet Server and ClientsThe Comet Server is a simple event handling and messaging component that accepts two types of connections:
It responds to the following Client events:
It sends the following events to any Clients that have subscribed to the related channel.
Both the Comet Server and the Client component, which Application Clients can be written with, are simple components. The Comet Server component is self-contained, the only interaction needed is to get it to start and stop, and it can be easily be hosted in a windows application, a service, or even in the Web Server itself. Similarly, the Client is a simple component (which the CometControl already encaptulates) which exposes some simple methods and events (on a single thread) which can easily be extended to write your own Application Clients which can also be hosted in a variety of ways. SummaryWeb browsers opening socket connections to a server isnt actually that new. In their isolated little sandboxes, Java applets have been doing it since they were invented more then a decade ago, and Flash has had the capability for while too. Microsoft's Silverlight will eventally be getting it in version 2.0. All the Comet aproaches are trying to do is get a similar capability directly within the web browser itself. Ive had fun with this project, exploring one Comet solution for ASP.NET. I dont pretend that its anything more than it is, a toy project, so consume and eat it with a grain of salt (and credit me if tastes nice). For this author however, it gave an interesting insight to whats possible. History7-Apr-2008 - First version!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||