Search |
||
Writing a RESTful and Comet based PubSub application using Atmosphere in less than 10 linesPosted by jfarcand on November 3, 2009 at 12:58 PM PST
Writing a publisher/subscriber (PubSub) is quite simple with Atmosphere using the atmosphere-jersey module. ![]() The main idea here is to use Comet for suspending the response when a client subscribe to a topic, and use REST for publishing messages to the those suspended responses. First, let’s bind our application to the root uri using the @path annotation
Next, let’s implement the subscribe operation by using the @Suspend and inject our Atmosphere’s Broadcaster using the @PathParam annotation:
The code above will be invoked when we want to create a new topic (@Path("/{topic}"), we will return a Broadcastable instance which will tell Atmosphere to use the passed Broadcaster, who got injected using the @PathParam, when broadcasting {topic}. Finally, the underlying response will be suspended forever via @Suspend annotation. Next, let’s implement the publish operation:
To publish, we just need to send a uri that takes the form of "/{topic}/{message}" as defined by @Path("/{topic}/{message}") and again, the Broadcaster we want to use to broadcast messagse will be injected based on the {topic} value. Finally, we just return a Broadcastable object and let Atmosphere broadcast the value to all suspended connections. That’s it! Now we can see it in action by doing: The source for the entire sample can be viewed here. Really simple, is it? For any questions or to download Atmosphere, go to our main site and use our Nabble forum (no subscription needed) or follow us on Twitter and tweet your questions there! technorati: atmosphere framework rest comet jersey ajax push »
Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|