title: Trigger server-sent-event on GET / POST in nodejs
body: <p>I have a mock backend in nodejs/express that I need to get working.  There is an SSE setup like this:</p>
  <pre class="lang-js prettyprint-override"><code>app.get(&quot;/api/sseConnect&quot; ,(req, res) =&gt; {
  
    headers = {
      &quot;Content-Type&quot;: &quot;text/event-stream&quot;,
      Connection: &quot;keep-alive&quot;,
      &quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;,
      &quot;Cache-Control&quot;: &quot;no-transform&quot;,
    };
    res.writeHead(200, headers);
  
    let intervalID = setInterval(() =&gt; {
      res.write(`data: ${JSON.stringify(Math.random())}\n\n`);
    }, 5000);
  
    res.on(&quot;close&quot;, () =&gt; {
      clearInterval(intervalID);
      res.end();
    });
  
  });
  </code></pre>
  <p>This is working great - the client hits this route, the connection is established, and the client recieves the message every 5 seconds.</p>
  <p>There are other standard routes, which when accessed, modify some data in the database.  Once the data is modified, I need to send a server-sent-event.  So for example:</p>
  <pre class="lang-js prettyprint-override"><code>app.post(&quot;/api/notifications/markasread&quot;, (req, res) =&gt; {
  
    let { ids } = req.body;
    ids.forEach(id =&gt; database.notifications[id].read = true) // or whatever
  
    // Send a server-sent-event with some information that contains id, message status, etc
  
  })
  </code></pre>
  <p>I know this seems really silly (why not just send a response???), but this is the way that the live api is set up - there is no response from this post route (or certain other routes).  They needs to trigger an SSE, which is listened for on the front end with an <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource" rel="nofollow noreferrer"><code>EventSource</code></a> instance.  Based on what is heard in the <code>eventSource.onmessage</code> listener, a whole bunch of things happen on the front end (react-redux).</p>
  <p>How can I 'hijack' the SSEs and trigger a response from a standard POST or GET route?</p>