Skip to main content

Unsubscribe from Streams

This page explains how you can unsubscribe from streams in Macrometa.

You must Install the Python SDK before you can run this code.

from c8 import C8Client

# Connect to GDN.
URL = "play.paas.macrometa.io"
GEO_FABRIC = "_system"
API_KEY = "XXXXX" # Change this to your API key

# Enter the consumer/subscription name which is subscribed to more than 2 streams
CONSUMER_NAME = "testConsumer"
IS_LOCAL = False # True for local stream and false for global stream

# True if you want to delete subscription from all local streams or False if you want to delete subscription on all global streams
IS_LOCAL_STREAMS = False

stream_name = "streamQuickstart"

client = C8Client(protocol='https', host=URL, port=443, apikey=API_KEY, geofabric=GEO_FABRIC)

if IS_LOCAL:
stream_name = f"c8locals.{stream_name}"
else:
stream_name = f"c8globals.{stream_name}"

type = ""
if IS_LOCAL_STREAMS:
type = "local"
else:
type = "global"

# Delete subscription (remove the given subscription from a particular stream)
resp = client.delete_stream_subscription(stream=stream_name, subscription=CONSUMER_NAME, local=IS_LOCAL)
print("Subscription deleted from stream ", stream_name, ": ", resp)

# Unsubscribe subscription (remove the given subscription from all streams (either global or local as specified by IS_LOCAL_STREAMS))
resp = client.unsubscribe(subscription=CONSUMER_NAME, local=IS_LOCAL_STREAMS)
print("Subscription unsubscribed (removed from all ", type, " streams): ", resp)