title: Server Sent Event from Spring Boot application on single server with multiple UI clients
body: <p>I am new to Server Sent Event concepts. Recently I added SSEemitter in my existing Spring Boot application, taking idea from internet.</p>
  <p>In-memory <code>SseEmitter</code> repository class is as follow:</p>
  <pre><code>@Repository
  public class InMemorySseEmitterRepository implements SseEmitterRepository {
  
    private static final Logger log = LoggerFactory.getLogger(InMemorySseEmitterRepository.class);
  
    private Map&lt;String, SseEmitter&gt; userEmitterMap = new ConcurrentHashMap&lt;&gt;();
  
    @Override
    public void addEmitter(String memberId, SseEmitter emitter) {
      userEmitterMap.put(memberId, emitter);
    }
  
    @Override
    public void remove(String memberId) {
      if (userEmitterMap != null &amp;&amp; userEmitterMap.containsKey(memberId)) {
        log.info(&quot;Removing emitter for member: {}&quot;, memberId);
        userEmitterMap.remove(memberId);
      } else {
        log.info(&quot;No emitter to remove for member: {}&quot;, memberId);
      }
    }
  
    @Override
    public Optional&lt;SseEmitter&gt; get(String memberId) {
      return Optional.ofNullable(userEmitterMap.get(memberId));
    }
  } 
  </code></pre>
  <p><code>SseEmitter</code> service class is as follows:</p>
  <pre><code>@Service
  public class SseEmitterServiceImpl implements SseEmitterService {
  
    private static final Logger log = LoggerFactory.getLogger(SseEmitterServiceImpl.class);
    private final SseEmitterRepository repository;
  
    public SseEmitterServiceImpl(@Value(SseEmitterRepository repository) {
      this.repository = repository;
    }
  
    @Override
    public SseEmitter subscribe(String entityId, EventType eventType) {
      SseEmitter emitter = new SseEmitter(-1L);        //SO THAT IT NEVER TIMES OUT
      String memberId = entityId + &quot;_&quot; + eventType;    //MEMBER ID HAS BEEN INTRODUCED BECAUSE FOR DIFFERENT ENTITIES, FOR DIFFERENT EVENTS WE CAN SEND EVENTS FROM SERVER TO CLIENT
      emitter.onCompletion(() -&gt; repository.remove(memberId));
      emitter.onTimeout(() -&gt; repository.remove(memberId));
      emitter.onError(e -&gt; {
        log.error(&quot;Create SseEmitter exception&quot;, e);
        repository.remove(memberId);
      });
      repository.addEmitter(memberId, emitter);
      return emitter;
    }
  } 
  </code></pre>
  <p>Associated resource class is as follows:</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/api&quot;)
  public class SseEmitterResource {
  
    private static final Logger log = LoggerFactory.getLogger(SseEmitterResource.class);
    private final SseEmitterService emitterService;
  
    public SseEmitterResource(SseEmitterService emitterService) {
      this.emitterService = emitterService;
    }
  
    @GetMapping(&quot;/subscribe/{eventType}&quot;)
    public SseEmitter subscribeToEvents(
        @PathVariable EventType eventType,
        @RequestParam(required = false, defaultValue = &quot;&quot;) String entityId) {
      log.info(&quot;Subscribing member with event type {} for entity id {}&quot;, eventType, entityId);
      return emitterService.subscribe(entityId, eventType);
    }
  }
  </code></pre>
  <p>Actually, there can be multiple event type and multiple entity ids for which we should be able to send events from Server to Client.</p>
  <p>The service class, responsible for sending the notification is as follows:</p>
  <pre><code>@Service
  public class SseNotificationServiceImpl implements SseNotificationService {
  
    private static final Logger log = LoggerFactory.getLogger(SseNotificationServiceImpl.class);
  
    private final SseEmitterRepository emitterRepository;
    private final DataMapper dataMapper;
  
    public SseNotificationServiceImpl(
        SseEmitterRepository emitterRepository,
        DataMapper dataMapper) {
      this.emitterRepository = emitterRepository;
      this.dataMapper = dataMapper;
    }
  
    @Override
    public void sendSseNotification(String memberId, MyDTO dto) {
      sendNotification(memberId, dto);
    }
  
    private void sendNotification(String memberId, MyDTO dto) {
      emitterRepository.get(memberId).ifPresentOrElse(sseEmitter -&gt; {
        try {
          sseEmitter.send(dataMapper.map(dto));
        } catch (IOException e) {
          ...
          emitterRepository.remove(memberId);
        }
      }, () -&gt; ...;
    }
  } 
  </code></pre>
  <p>Finally, the place, where I send this Server Sent Event notification is as follows:</p>
  <pre><code>sseNotificationService.sendSseNotification(...);
  </code></pre>
  <p>Now, my questions are as follows:</p>
  <ol>
  <li><p>if there are multiple, say 6 clients for the Spring Boot application deployed on single POD, will all of them receive notification through the single <code>SseEmitter</code> instance, as I have shown above ?</p>
  </li>
  <li><p>If not, could anyone please help me with the changes which I need to incorporate in the above code, in order to send Server Sent Event notification to all clients ? Thanks.</p>
  </li>
  </ol>