title: SocketIO Communication with a Gateway Between the Client and a Service?
body: <h2>Gist</h2>
  
  <p>I have an application that runs on an microservice-based architecture (on Kubernetes). All the communication to/from outside the application happens through an <strong>API Gateway</strong>.</p>
  
  <p>Which just means that requests from my frontend don't go directly to the services, but they have to go through the Gateway.</p>
  
  <h2>Motive</h2>
  
  <p>Now I need to implement a feature that requires realtime communication between the frontend and an internal service. But since the internal service is not exposed to the outside, I need a way to "route" the realtime data through the Gateway.</p>
  
  <p>All my services are running on Node.js, which is the reason I want to use <a href="https://socket.io/" rel="noreferrer">Socket.IO</a> to implement the realtime communication.</p>
  
  <p><a href="https://i.stack.imgur.com/XFuJo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XFuJo.png" alt="architecture"></a></p>
  
  <h2>Issue</h2>
  
  <p><strong>But how to implement the purple double arrow from the sketch?</strong></p>
  
  <p>So usually the frontend client would connect to the server where Socket.IO is running. But in my case this server (the realtime feature server) is not accessible from the client (and never should be), which means that the client has to connect to the Gateway. Thus the Gateway needs to implement some mechanism to route all incoming messages to the realtime service and vice verca.</p>
  
  <h2>Ideas</h2>
  
  <p>(1) Have a second HTTP server listening for events on the Gateway and emit those event to the realtime server. In the other direction, the realtime server will emit events to the Gateway, which wiil then emit them to the frontend. I think this approach will definitely work, but it seems redundant to emit everything twice. And it would definitely hurt performance?</p>
  
  <p>(2) Use a Socket.IO Adapter to "<a href="https://github.com/socketio/socket.io-website/blob/master/source/docs/using-multiple-nodes.md#passing-events-between-nodes" rel="noreferrer">pass event between nodes</a>", which seems as the right way to go because it is used to "pass messages between processes or computers". But I have problems getting started because of the lack of documentation / examples. I am also not using Redis (is it needed to use the adapter?)</p>
  
  <p>(3) Use the <a href="https://github.com/socketio/socket.io-emitter" rel="noreferrer">socket.io-emitter</a> package, which seems not like a good option since the last commit was from 3 years ago.</p>
  
  <p>(4) Something else?</p>