Absinthe subscriptions: Unsubscribe from all topics

Hi all,

How can I unsubscribe from all topics (Absinthe subscriptions) upon logout?

Frontend (React & Apollo):

const logoutMutation = gql`
  mutation {
    logout
  }
`;

Backend (Phoenix & Absinthe):

def logout(user_id) do
  ...
  unsubscribe_all_topics(user_id)
  ...
end
def unsubscribe_all_topics(user_id) do
???
end

Subscriptions exist only as long as the websocket is alive. You can either track the various subscription handles you get back from the subscribe call, and call unsubscribe with each of them, or you can simply disconnect the websocket connection you sent the subscriptions over, and that will also automatically terminate all of them.

4 Likes

@benwilson512 Thanks a lot for your assistance!:smile:

@benwilson512 Do you mean call Absinthe.Subscription.unsubscribe(pubsub, subscriptionId)? I tried that from my application code but it doesn’t work. I modified Absinthe.Subscrpition locally and tested unsubscribe, and it works as expected there. Any ideas on why it might not work from my application code? I’ve confirmed that I’m passing exactly the same values to unsubscribe in both cases.