From a03a7467f1181f03caf551c41343cc1d627d833f Mon Sep 17 00:00:00 2001 From: m0rt3nlund Date: Mon, 14 Oct 2024 10:18:13 +0200 Subject: [PATCH] Added unsubscribe function that matches on metadata This allows multiple subscriptions to the same topic and later the ability to remove a specific subscription instead of "all" subscriptions for the topic --- lib/phoenix/pubsub.ex | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/phoenix/pubsub.ex b/lib/phoenix/pubsub.ex index 6ae950da..44ae7b0b 100644 --- a/lib/phoenix/pubsub.ex +++ b/lib/phoenix/pubsub.ex @@ -132,6 +132,17 @@ defmodule Phoenix.PubSub do Registry.unregister(pubsub, topic) end + @doc """ + Unsubscribes the caller from the PubSub adapter's topic taking the metadata into consideration + Unlike `unsubscribe/2` this function match on the metadata provided as option when subscribed + + This is usually needed if you have multiple subscriptions for the same topic with different metadata + """ + @spec unsubscribe(t, topic, metadata) :: :ok + def unsubscribe(pubsub, topic, metadata) when is_atom(pubsub) and is_binary(topic) do + Registry.unregister_match(pubsub, topic, metadata) + end + @doc """ Broadcasts message on given topic across the whole cluster.