title: Displaying alert message to user from spawner
body: <p>We have written a custom spawner for JupyterHub to suit our use cases. In the same spawner, we have memory limits for every user, and this is checked within the <code>poll()</code> function, and logged on the server.</p>
  
  <p>What I want to do is to display an alert to the client when he approaches, say about 90% of his memory limits, and another at 100%, displaying that the process is being killed.</p>
  
  <p>Simply put : <strong>I need to display an alert message on client browser window through the <code>poll()</code> function</strong></p>
  
  <p>Is it even possible ? Can I do it using Flask ? (The resources I found online were about creating a new server. In this case, we already have a jupyterhub server running.)</p>
  
  <p><strong>EDIT</strong> : Here's my client side Javascript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  // Client-side Javascript in the HTML
  
  var targetContainer = document.getElementById("this-div");
  var eventSource = new EventSource("/stream");
  eventSource.onmessage = function(e) {
      targetContainer.innerHTML = e.data;
  };
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Here's the <code>poll()</code> function :</p>
  
  <pre><code>@gen.coroutine
  def poll(self):
      """Poll the spawned process to see if it is still running.
  
      If the process is still running, we return None. If it is not running,
      we return the exit code of the process if we have access to it, or 0 otherwise.
      """
  
      self.log.debug("Polling " + self.user.name + "...")
      # peh = ProxyErrorHandler()
      # peh.get(503)
      self.stream()
  </code></pre>
  
  <p>And here's the <code>stream()</code> function:</p>
  
  <pre><code>app = Flask(__name__)
      @app.route("/stream")
      def stream(self):
          def eventStream():
              while True:
                  yield "Hello World"
          return Response(eventStream(), mimetype="text/event-stream")
  </code></pre>
  
  <p>The Javascript throws a <code>404 Not Found</code> error, which makes sense because the poll() function runs every 30 seconds only.</p>