title: dealing with server side event disconnections
body: <p>I have a simple python bottle application that emits SSE events to a javascript listener. </p>
  
  <p>This works without problems until the client 'goes away' via a browser refresh or page back etc.</p>
  
  <p>Because the events from the application are delivered and then removed from an internal queue, one or two events are lost when the server tries to send them to a client that is no longer there.</p>
  
  <p>I don't want the events to be lost, but I cannot currently see a way to fix this in a sensible manner.</p>
  
  <p>I plan to switch to web sockets instead and use an event - acknowledgement model to solve the problem unless there is something I could do with the SSE to prevent the events from being lost in this way.</p>
  
  <p>It seems to me that SEE may not be designed for my particular use case in mind.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>@route('/stream/events')
  def event_stream():
      response.content_type  = 'text/event-stream'
      response.cache_control = 'no-cache'
      # Set client-side auto-reconnect timeout, ms.
      yield 'retry: 1000\n\n'
  
      while True:
          event = event_queue.get()
          logging.debug("Received event from hal: %s", event)
          yield "data: " + json.dumps(event) + "\n\n"
  </code></pre>
  
  <p>A link to my solution for the problem can be found here. It uses weak references to 'detect' when a client has gone away, aswell as individual queues per SEE connection: <a href="http://blog.jason.spashett.com/python-bottle-server-side-events-one-way-to-handle-client-disconnects.html" rel="nofollow">http://blog.jason.spashett.com/python-bottle-server-side-events-one-way-to-handle-client-disconnects.html</a></p>