How Can I Manually Unsubscribe Clients From My Server?

The short answer is it depends. The long answer is... If you are utilising our user authentication feature, calling signIn on clients, then you can use the terminateUserConnection endpoint to remove any connections associated with that user. If you are not using this feature and clients don't invoke signIn then it is not possible to unsubscribe clients from the server side.

You can of course include logic in your clients so that you can tell them to unsubscribe themselves (`pusher.unsubscribe("some-channel")`), however this doesn't protect against malicious clients who might want to continue to listen in on a channel longer than you would like them to. A workaround for this is the following:

  • Have each client subscribe to a unique private channel, for their ears only.

  • Send any events destined for a particular client on their unique private channel.

Now if you wish to stop a client listening in on events, that is as simple as stopping sending them any – it doesn't matter if they stay subscribed since they won't overhear any events that they shouldn't.

You can simulate traditional multiple-client channels by

  • Keeping a list of clients for each of your virtual channels.

  • Every time you want to send an event to some virtual channel, look at the list of clients, and send the event on each of their unique private channels.

  • "Unsubscribing" a client from this channel is then just a case of removing their name from the virtual channel members list.

If you would like to control who connects to your application then you can use the authorized connections feature, which means malicious users can't create connections without you first authorizing them.

Still have questions? Please reach out to our Support team by visiting this page.

Last updated