Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically what you need is to implement <a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern" rel="noreferrer">Publish-subscribe</a> on top of Atmosphere.</p> <p>Atmosphere consists of two parts: client-side (javascript-based) and server-side(java-based).</p> <p>First of all you need to configure server-side: <a href="https://github.com/Atmosphere/atmosphere/wiki/Installing-Atmosphere" rel="noreferrer">Installing Atmosphere</a></p> <p>Namely servlet or filter, it is required so that it could add <a href="https://github.com/Atmosphere/atmosphere/wiki/Understanding-AtmosphereResource" rel="noreferrer">AtmosphereResource</a> to the <em>HttpServletRequest</em>. </p> <p><a href="https://github.com/Atmosphere/atmosphere/wiki/Understanding-AtmosphereResource" rel="noreferrer">AtmosphereResource</a> represents a single client connection on the server-side.</p> <p><em>Broadcaster</em> is actually a container for these resources, so that you don't need to handle lookup/iteration/concurrency when you need to send to multiple connections. (Note that multiple connections can be produced by single client).</p> <p>On the server-side you need to provide clients an endpoint to subscribe for notifications. For example, if you are using Spring-MVC, it could go like this (omitting validations/authentications, etc.):</p> <pre class="lang-java prettyprint-override"><code>@RequestMapping(value = "/user-notifications/{userId}") @ResponseStatus(HttpStatus.OK) @ResponseBody public void watch(@PathVariable("userId") String userId, HttpServletRequest request) throws Exception { //Atmosphere framework puts filter/servlet that adds ATMOSPHERE_RESOURCE to all requests AtmosphereResource resource = (AtmosphereResource)request.getAttribute(ApplicationConfig.ATMOSPHERE_RESOURCE); //suspending resource to keep connection resource.suspend(); //find broadcaster, second parameter says to create broadcaster if it doesn't exist Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(userId,true); //saving resource for notifications broadcaster.addAtmosphereResource(resource); } </code></pre> <p>When something happens you can notify clients like this:</p> <pre><code>public void notify(User user, Event event){ Broadcaster b = BroadcasterFactory.getDefault().lookup(user.getId()); if (b!=null){ b.broadcast(event); } } </code></pre> <p>On the client side you need to send a subscribe request and listen for subsequent events, like this:</p> <pre class="lang-javascript prettyprint-override"><code>var request = new atmosphere.AtmosphereRequest(); request.url = '/user-notifications/'+userId; request.transport = 'websocket'; request.fallbackTransport = 'streaming'; request.contentType = 'application/json'; request.reconnectInterval = 60000; request.maxReconnectOnClose = 1000; request.onMessage = function(response){ console.log(response); alert('something happend&lt;br&gt;'+response); }; that.watcherSocket = atmosphere.subscribe(request); </code></pre> <p>So, to sum it up:</p> <ol> <li>Client sends request "I want to receive this kind of notifications".</li> <li>Server receives request, suspends and saves connection somewhere (either in your code or in Broadcaster).</li> <li>When something happens server looks for suspended connection and sends notification in it.</li> <li>Client receives notification and callback is invoked.</li> <li>Profit!!!</li> </ol> <p><a href="https://github.com/Atmosphere/atmosphere/wiki" rel="noreferrer">This wiki</a> has explanations for some concepts behind Atmosphere and links to other documentation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload