remove unsubscribed subscriptions

800 Views Asked by At
  • Laravel6
  • lighthouse-php5

I'm using lighthouse-php for GraphQL Subscription and I receive the message from pusher.

So I have the question of Subscription unsubscribe.

server side => pusher => front side

Documantation this part means just stop pusher => front side. If I don't remove the redis, still server side working and push message to pusher, right?

  unsubscribeFromChannel(subscriptionChannel) {
    this.pusher.unsubscribe(subscriptionChannel);
  }

I want to stop server side => pusher too. What options I have?

thanks

1

There are 1 best solutions below

8
On BEST ANSWER

This is documented in the Lighthouse documentation, a direct link here: https://lighthouse-php.com/5/subscriptions/getting-started.html#expiring-subscriptions.

But also some snippets here just in case it helps.

Subscriptions do not expire by themselves. Unless you delete a subscription, it will continue to broadcast events after the client has disconnected.

The easiest way to expire subscriptions automatically is to use the env LIGHTHOUSE_SUBSCRIPTION_STORAGE_TTL to set an expiration time in seconds (e.g. LIGHTHOUSE_SUBSCRIPTION_STORAGE_TTL=3600 to expire in one hour).

So setting this .env variable is a great way to expire subscriptions that are left behind automatically, do set this timeout large enough, if you have a lot of long running subscriptions an hour might not be enough.

But since you are using Pusher there is a faster way to cleanup the subscriptions:

If you are using the Pusher driver, you can use a Presence webhook to mitigate this problem. When a Pusher channel is abandoned (ie. unsubscribed), it will trigger the webhook, which will instruct Lighthouse to delete the subscription.

This webhook (channel existence webhook as Pusher calls them) will remove subscriptions for channels that are vacated (no subscribers left) this will happen a few seconds after the user disconnects keeping the subscriptions in Redis up-to-date.


So long story short, setup a TTL for the subscriptions as a fallback and configure the Pusher webhook for your app. This will keep the subscriptions stored up-to-date and prevent unneeded work.