Publish or Send data over existing client websocket?

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?

extended information:

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.

Help please!

Hi @luxfx_fp :wave: welcome to the forum! It should be possible to send queries and mutations over WebSockets, though I haven’t tried it myself. See this comment:

I recommend taking a look at your terminating link and taking a closer look at what link is handling your mutations. Feel free to paste it here if you’d like a second set of eyes!

Thanks for the welcome!

That’s an interesting idea! I hadn’t considered pushing all traffic through socket… hm. I’ll have to weigh the benefits. Or find a way to tune the split I’m using so include the specific mutation that carries the terminal commands to use the socket.

This gives me something to work with, I appreciate it.

1 Like