Home > Atmosphere, Comet, Websocket > Trick of the Friday #1: Delivering Server Side Events to your Websocket/Comet Application.

Trick of the Friday #1: Delivering Server Side Events to your Websocket/Comet Application.

When writing asynchronous web application (Comet and/or Websocket based), most of the time you need to generate server sides events from external components or non web-based technology.

As an example, you may have EJBs, JMS queue/topic that may need to deliver server sides events to your set of asynchronous connections. With Atmosphere, it is quite easy to generate server sides events using Broadcaster. A Broadcaster role is to push back server sides events to the Browser. Within the scope of a Web application, all you need to do is:

// Retrieve the default Broadcaster

Broadcaster broadcaster = atmosphereResource.getBroadcaster();

// Or create your own

Broadcaster broadcaster = new Broadcaster(“myBroadcaster”);

// Or let Atmosphere injects it for you like:


public void broadcast(@PathParam(“myBroadcaster”) Broadscaster broadcaster);

This return the Broadcaster associated with the current asynchronous connection (Websocket or Comet). A Broadcaster can be shared amongst all your connections (chat like application) or to a subset based on some topic (pub sub, etc). The Broadcaster concept is really close to an events queue Browser can subscribe to and received asynchronous events.

But what about non web components (EJB, JMS, Akka/Scala Actor, etc.) or web component with different scope (another web application, another Servlet, etc.)? They can also generate server sides events by using Atmosphere’s BroadcasterFactory

Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, “id_of_the_broadcaster”);

// Or

Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(JerseyBroadcaster.class, “id_of_the_broadcaster”);

broadcaster.broadcast(‘Hello from an EJB”);

By default, the value of “id_of_the_broadcaster” is based on the  Atmosphere’s <url-pattern> value (ex:/*). You can always set that value by calling:


What about if you don’t know the name of your Broadcaster? You can also retrieve all available Broadcasters by doing:

// An unmodifiable collection

Collection<Broadcaster> broadcasters = BroadcasterFactory.getDefault().lookupAll();

In conclusion, being able to generate server sides events from external components is a mandatory features required when writing asynchronous application. Before picking a framework for writing an asynchronous application, always make sure such features is available to avoid having to write it yourself (and maintain it).

For any questions or to download Atmosphere Client and Server Framework, go to our main site and use our Nabble forum (no subscription needed), or follow the team or myself and tweet your questions there! You can also checkout the code on Github. Or download our latest presentation to get an overview of what the framework is.

Categories: Atmosphere, Comet, Websocket
  1. Ganesh
    June 25, 2010 at 5:31 am

    This is great.
    Two small things though,
    1) using jQuery one may run in to problem with “$” alias when using other libraries like prototype etc..,. I see that you append window.parent.$.atmosphere.streamingCallback in your serverside filters.
    If someone uses the “jQuery.noconflict()” in their client side library, they would run in to trouble.
    2) In jQuery.atmosphere.js when framing the url for the websocket instead of
    var location = url.replace(‘http:’, ‘ws:’);
    use var location = url.replace(‘http’, ‘ws’);
    if its over ssl ws would be substituted as wss.


  2. Stephane
    January 10, 2013 at 12:41 pm


    A few questions still:

    1- I understand Broadcaster is an interface. When you write:
    Broadcaster broadcaster = new Broadcaster(“myBroadcaster”);
    you mean it as a shortcut for a:
    Broadcaster broadcaster = new SimpleBroadcaster(“myBroadcaster”);

    2- Is the setID using the same id as the one passed in the constructor ?
    Broadcaster broadcaster = new SimpleBroadcaster(“ForAkkaActorWho”);

    3- If it is possible to create a broadcaster with a:
    Broadcaster broadcaster = new SimpleBroadcaster(“myBroadcaster”);
    how then can it be retrieved using a url pattern ?

    Kind Regards,

    • January 10, 2013 at 1:16 pm

      Salut, this blog is kind of hold. Instead, take a look at this documentation. In short, create broadcaster using BroadcasterFactory.getDefault().lookup(….) API instead of directly creating it. Let’s continue the discussion on the mailing list.

  3. February 19, 2015 at 12:33 pm

    -open [client (http/create-client)]
    (let [response (http/GET client “http://github.com/neotyk/http.async.client/”)]
    (-> response

  1. June 24, 2010 at 7:39 pm
  2. July 2, 2010 at 2:50 pm
  3. October 1, 2010 at 4:06 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: