My use-case requires bidirectional real-time communication. But all of the documentation and examples for Apollo sockets (either graphql-ws or subscriptions-transport-ws) are for subscriptions to receive data from the server only.
How do I continue to use the opened websocket?
I’m updating a feature written by another developer before I arrived at the company. We have Apollo on client (React) and server. The server side is acting as a proxy to allow for terminal access (xterm.js / node-ssh) between the frontend and an isolated behind-the-firewall machine.
The current system works… but I don’t like it. Frontend connects via graphql subscription, the server creates an ssh connection to the isolated machine, and on feedback through the ssh pipeline will pass the information back to the frontend through the subscription. This part works as I think it should.
But the input from the frontend is going through a mutation and saved into a mongodb. The server – inside the controller that has opened the ssh pipeline – is watching for changes to that specific mongo collection, and passes changes on to the isolated machine. Using a database for state management like this is crazy. But the server doesn’t maintain state otherwise, so I can’t think of another way to send mutation commands through the ssh pipeline.
From what I know of websockets (not much, and not through experience), they are supposed to be bidirectional, and supports sending/publishing new information over existing sockets. But I can’t figure out how to do this with Apollo.