Serverside events or subscriptions with ApollographQL


My team and I are running a Apolloserver for our app and we have a feature request that requires us to use subscriptions or server-sent events. In short we need to intercept orders from multiple clients and send them as print requests to a client that is connected to the printer.

I’ve got something to work with Apollo 2 and the included PubSub class, but with Apollo 3 I haven’t yet been able to create a working solution. I’ve been down the rabbit hole with Apollo 2 vs Apollo 3 and apollo-server-express with subscription-transport-ws or graphql-ws, but it just feels like I’m pulling random levers.
I’m also pretty new to subscriptions, so that doesn’t exactly help my case.

Is there some “best practices” for setting up subscriptions / sse with Apollo?
Can I use the query and mutation part of my api over HTTP (as normal), even if I have implemented subscriptions?
Is there a way to make the Apollo playground work with subscriptions or sse?

Can anyone share some experience with this?

Depends on if you’re using federation or not. If you’re using federation, this post by Mandi Wise is the suggested way. Otherwise you can just use graphql-ws, subscription-transport-ws is no longer maintained.

Is there a way to make the Apollo playground work with subscriptions or sse?

The Apollo Playground doesn’t work with graphql-ws yet, but there’s a couple open PR’s that add support for it you can run locally (1295 or 1326).

Thanks for the reply!
I actually read the post about federation, but we don’t use federation (yet) so i didn’t bother much with that.
I got the server running using the references in the article about setting up subscriptions, but I don’t quite understand how this runs…
For instance, the subscription-transport-ws is created in the init server function, and from the docs this is needed to manage the connection, authentication and transport. But i can’t figure out how to stick this into the http.createserver og express app, yet the server starts fine and the subscriptions works ( without authentication at least) even when the subscription-transport-ws is commented out.

You can check out the codeSandbox in the article and try it for yourself.