title: Atmosphere: Multiple Clients for Managed Service
body: <p>We have an embedded Jetty 9 application that we are trying to add Amosphere support to. The goal is to provide real-time data to AngularJS-based web application. We are using Atmosphere 2.1.3 (though we did try 2.2.0-SNAPSHOT as well).</p>
  
  <p>The issue right now is that while the application works perfectly with one client, it fails with multiple clients: the first client stops and the new one takes over.</p>
  
  <p>Here is the code that initializes Atmosphere in the server (class that instantiates and starts Jetty):</p>
  
  <pre><code>    ServletHolder atmosphereServletHolder = new ServletHolder(AtmosphereServlet.class);
      atmosphereServletHolder.setAsyncSupported(true);
      serviceContext.addServlet(atmosphereServletHolder, "/atmosphere/*");        
  </code></pre>
  
  <p>Here is the Managed Service code (based on the chat example):</p>
  
  <p>Annotations:</p>
  
  <pre><code>@ManagedService(path = "/atmosphere/values")
  @Singleton
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>private static final Logger LOGGER = LoggerFactory.getLogger(MyService.class);
  
  @Ready(value = Ready.DELIVER_TO.ALL)
  public final void onReady(final AtmosphereResource atmosphereResource){
      LOGGER.info("Browser {} connected.", atmosphereResource.uuid());
  }
  
  @Disconnect
  public final void onDisconnect(final AtmosphereResourceEvent atmosphereResourceEvent){
      final String uuid = atmosphereResourceEvent.getResource().uuid();
      if(atmosphereResourceEvent.isCancelled()) {
          LOGGER.info("Browser {} unexpectedly disconnected", uuid);
      } else if(atmosphereResourceEvent.isClosedByClient()) {
          LOGGER.info("Browser {} closed the connection", uuid);
      }
  }
  
  @Message(encoders = {MessageEncoderDecoder.class})
  public ListResponse list(Object test) {
      ...
  
      return new ListResponse(...);
  }
  </code></pre>
  
  <p>Here is most of the client side (again, mostly based on the angular example). The data pushed into socket is meaningless since there are no arguments to the service:</p>
  
  <pre><code>    $scope.model = {
          transport: 'websocket',
          accounts: {}
      };
  
      var request = {
          url: '/atmosphere/values',
          contentType: 'application/json',
          logLevel: 'debug',
          transport: 'websocket',
          trackMessageLength: true,
          reconnectInterval: 5000,
          enableXDR: true,
          timeout: 600000
      };
  
      request.onOpen = function (response) {
          $scope.model.transport = response.transport;
          $scope.model.uuid = response.request.uuid;
          $scope.model.connected = true;
          $scope.model.content = 'Atmosphere connected using ' + response.transport;
          socket.push(angular.toJson({ test: 'test' }));
      };
  
      var socket = null;
      request.onClientTimeout = function (response) {
          $scope.model.content = 'Client closed the connection after a timeout. Reconnecting in ' + request.reconnectInterval;
          $scope.model.connected = false;
          setTimeout(function () {
              socket = atmosphereService.subscribe(request);
          }, request.reconnectInterval);
      };
  
      request.onReopen = function (response) {
          $scope.model.connected = true;
          $scope.model.content = 'Atmosphere re-connected using ' + response.transport;
          socket.push(angular.toJson({ test: 'test' }));
      };
  
      ...
  
      request.onMessage = function (response) {
          var responseText = response.responseBody;
          try {
              $scope.model.values = angular.fromJson(responseText);
              socket.push(angular.toJson({ test: 'test' }));
          } catch (e) {
              $log.error("Error parsing JSON: ", responseText);
              throw e;
          }
      };
  
      ...
  
      socket = atmosphereService.subscribe(request);
  </code></pre>
  
  <p>So, what is missing to make this respond to all the clients and not just one?</p>
  
  <p>Thanks!</p>