title: Issue with sending SSE from express
body: <p>We are using microservices architecture for our project. Our project is similar to a blog. There is an <strong>activities</strong> service, which logs all the activities done by the user, like, adding post, comment, replying to a comment and so on.</p>
  
  <p>Now for each activity, we need to send an SSE notification to the involved users. For this, we are using another service called <strong>notifications</strong>. So whenever an activity occurs, an HTTP request will be sent to notifications service which handles sending various SSE events.</p>
  
  <p>However, we are facing some issues in sending SSE. The two major issue we are facing are <strong>memory leak</strong> and an error <strong>Error: write after end</strong></p>
  
  <p><strong>Route</strong></p>
  
  <pre><code>router
      .route("/")
      .get(controller.eventStream)
      .post(controller.sendNotification);
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>import axios from "axios";
  import eventEmitter from '../services';
  
  const controller = {
    eventStream: (req, res, next) =&gt; {
      console.log("Inside event stream");
      res.writeHead(200, {
        "Content-Type": "text/event-stream",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive"
      });
  
      eventEmitter.on("sse", (event, data) =&gt; {
        console.log('Event Triggered');
        res.write(`event: ${event}\ndata: ${JSON.stringify(data)} \n\n`);
      });
  
      req.on("close", () =&gt; {
        console.log("Inside Close");
        res.end();
      });
    },
  
    sendNotification: (req, res, next) =&gt; {
      try {
        const {
          userId,
          action,
          type,
          item_id,
        } = req.body;
  
        // First check the type of activity that has been performed
        switch (type) {
          case "topic":
            // Then check the type of action that has been done
            switch (action) {
              case "edit":
                console.log("Topic edited");
                const data= 'John Doe has edited a topic';
                eventEmitter.emit("sse", `edit-topic`, data);
                break;
            }
            break;
        }
        res.send('sse successfully send');
      } catch (error) {
        res.status(500).json({error: 'SSE failed'});
      }
    }
  };
  
  export default controller;
  </code></pre>
  
  <p><strong>Service</strong></p>
  
  <pre><code>export default new events.EventEmitter();
  </code></pre>
  
  <p>Initally client side will send a <strong>GET</strong> request which executes the <code>eventStream</code> controller.</p>
  
  <p>Now for each activity, an SSE needs to be sent through this stream</p>
  
  <p>I believe <strong>Error: write after end</strong> is because the event is triggered after sending a respose. This can be fixed by removing <code>res.send('sse successfully send');</code> However, node will throw the timeout error.</p>
  
  <p>I am not sure how to send an SSE from <code>sendNotification</code> controller. Moreover, I am not sure whether this is the right approach. Any guidence would be much appreciated.</p>