title: Sharing components between views - how to improve my design?
body: <p>I'm working on a JSF webapp which purpose is to wrap a command-line program.
  One of its main functionality is the ability to share a session between users (eg. for courses purpose), so that when an input is sent to an instance of the application, the output sent to every subscriber for this session.</p>
  
  <p>As a result of this design, the webapp is mainly composed of a view-scoped bean which will request a controller of the command-line application.
  It has also been chosen to identify a session with the URL fragment (eg. mydomain/myapp/#SESSIONID), so that anyone using the URL with the same fragment will share inputs and outputs, <strong>using its own instance of the view-scoped bean but sharing the same controller</strong></p>
  
  <p>In order to push results to all subscribers, I'm using Primefaces Push. Results are primarily text that has to be appened to the webapp's terminal, but some commands lead to the programmatic creation of a JSF component.
  In order to handle this, I just render these components to a string that I send to all subscribers.</p>
  
  <p>Then, I realized that in order to handle ajax requests from components (and from every subscriber), the associated UIComponent needs to be added to the UIViewRoot <em>in the context of</em> (don't know how to express this) each view-scope bean.</p>
  
  <p>As a matter of fact, I first tried to bind a "common container" (a UIForm) to a property of the view scoped bean, in which I would put the programmatically created components, but I obviously had to face the chicken/egg issue @BalusC <a href="http://balusc.blogspot.fr/2010/06/benefits-and-pitfalls-of-viewscoped.html" rel="nofollow noreferrer">talks about in his blog</a>, because the component was added again on each ajax request. Setting <code>javax.faces.PARTIAL_STATE_SAVING</code> to false didn't help either (I'm using <em>MyFaces 2.2.5</em>)</p>
  
  <p>So, as somewhat of a workaround, when the controller needs to create a new component, it basically adds the id of the component to the data pushed (in a HashMap converted to Json), and all subscribers will trigger (back) a remoteCommand to its own instance of the view-scoped bean, in order to update the "common container" from its own UIViewRoot.</p>
  
  <p>This does work, but I don't like this way of doing it!</p>
  
  <p>So:</p>
  
  <ol>
  <li><p>would it be possible to handle this kind of sharing between view-scope beans (with the same name) which are stored in different HTTP sessions? I'm refering to <a href="https://stackoverflow.com/questions/6915055/are-jsf-views-shared-between-users">this answer</a> from @BalusC... maybe playing with <code>javax.faces.ViewState</code> - would it even be possible?</p></li>
  <li><p>Is there a "magical" scope for my currently-view-scoped bean I could use?</p></li>
  <li><p>Shall I rather use a completely different design?</p></li>
  </ol>
  
  <p>Thanks!</p>