<p>I'm running a SSE server on a VPS and it works perfectly, no problems at all, but due to scalability reasons I needed to move it to another server.</p>

<p>I moved the server to <strong>Google Cloud Platform/Google Container Engine</strong> and <strong>Kubernetes/Ingress</strong>. But now I've encountered that I can't keep a SSE connection effectively, it's completely unstable and it closes connections by itself.</p>

<p>Is there anything special I have to do to run a <strong>SSE</strong> server over <strong>Kubernetes/Ingress</strong>?</p>

<p>I assume my code/software runs perfect and that is not the issue, due it works perfectly in Kubernetes, VPS, on my machine, everywhere, just not when I add the Ingress configuration, and I'm doing this because I want <strong>HTTPS</strong> over the <strong>Kubernetes</strong> <strong>load-balancer</strong>.</p>

<p>I'ts not possible to do it, it may work for some clients and not for others, the explanation of this is with how kubernetes behave, <a href="https://i.stack.imgur.com/3Kcx5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Kcx5.jpg" alt="enter image description here"></a>
let's supposed there are 3 nodes in your app, a node is virtual machine, when a request it's being made, kubernetes has to decide in which of this virtual machines will run, if it's the case of <a href="https://www.w3schools.com/html/html5_serversentevents.asp" rel="nofollow noreferrer">SSE</a> this conection must persists until client close your app, so with this in mind, let's supossed you have a chat app.
So we have two clients, now let's supossed kubernetes always redirect client 1 and 2 to node 1 <a href="https://i.stack.imgur.com/J04a4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J04a4.png" alt="enter image description here"></a></p>

<p>when a SSE connection(green line) is stablished, this will persists, now let's supossed client 1 send a message, as I said before we are in the case everything goes through node 1, 
<a href="https://i.stack.imgur.com/zCQrR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zCQrR.png" alt="enter image description here"></a>
the node will handle this request, your app will look in memory if receiver is connected and if so, it will send the message through SSE conection <a href="https://i.stack.imgur.com/33Baj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/33Baj.png" alt="enter image description here"></a> in this case everything works perfect, the problem is when the clients are not redirected to the same node 
<a href="https://i.stack.imgur.com/wZDcT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wZDcT.png" alt="enter image description here"></a></p>

<p>when a client sends a message,<a href="https://i.stack.imgur.com/9ESvU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9ESvU.png" alt="enter image description here"></a>
this will go through to node 1 but when checking if client 2 is connected, it won't find the conection because this connection was not saved in this node so the client 2 won't recieve this message. To solve this problem you will need a new structured, you need to have one instance dedicated only for SSE conections, so when a client conects to a sse it will go to a the dedicated instance <a href="https://i.stack.imgur.com/yqGpS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yqGpS.png" alt="enter image description here"></a> 
and when client 1 sends a message <a href="https://i.stack.imgur.com/R6uxa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R6uxa.png" alt="enter image description here"></a>
the nodes will comunicate with this dedicated instance and will send to client 2 the message <a href="https://i.stack.imgur.com/kXcAY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kXcAY.png" alt="enter image description here"></a></p>

<p>now to created this dedicate server you should use and external source service if you want, or better you could create your own instance and redirect your sse endpoint to the SSE instance, you can do this with a Load Balancing this acts as a proxy and it will decide to which resource redirect based on the incoming URL <a href="https://cloud.google.com/load-balancing/docs/https/url-map" rel="nofollow noreferrer">more info</a>
Also you need to configure in this dedicate SSE server not to close connection to fast </p>
