# Anyway to subscribe to s/us topic from mqtt client to receive events directly

**URL:** <https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [May 19, 2024, 2:22pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975 "2024-05-19T14:22:38Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 19, 2024, 2:22pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/1 "2024-05-19T14:22:38Z")

</div>

#### _ **Product/components** used and **version/fix** level:_

Cumulocity/MQTT

#### _Detailed explanation of the problem:_

I have code on the edge device that publishes cumulocity events to Cumulocity on s/us topic using the 401 event template. Is there a way to subscribe to s/us topic from my Python Cumulocity microservice so I can can react to the event as quickly as Cumulocity can.  
The reason I’m doing this is so I don’t have to do a search for Cumulocity event in a polling fashion.  
Note, I can’t change the edge code to publish to a different topic since I don’t own the code.

#### _Error messages / **full error** message screenshot / **log** file:_

#### _Question related to a **free trial** , or to a **production (customer)** instance?_

Partner tenant

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [May 19, 2024, 3:57pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/2 "2024-05-19T15:57:37Z")

</div>

Hi Eddie,

you cannot subscribe to s/us. However, there is a different solution for your use case. Once the platform received/created your event, a realtime notification is produced. You can subscribe to this Event-Stream from your Microservice via the Notifications API.

Few links:

- [About Notifications](https://www.cumulocity.com/api/core/#tag/About-notifications-2.0) from official docs
- [Notification 2.0 step-by-step](https://community.cumulocity.com/t/step-by-step-instruction-for-cumulocity-notification-2-0/3993) knowledge base article
- Here is an [example](https://github.com/Cumulocity-IoT/cumulocity-python-api/blob/main/samples/notification2_synchronous.py) using [this](https://github.com/Cumulocity-IoT/cumulocity-python-api) Python client.

---

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 20, 2024, 2:26pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/3 "2024-05-20T14:26:32Z")

</div>

Thanks Korbinian. This sounds encouraging. But I’d like you to consider the specific use case of being able to detect if I receive any telemetries from any of my devices and then be able to process them according to that device. It will be impossible to use the “mo” context as then I can only have one subscription per device which if allowed will be unmanageable if I have hundreds of devices. Any possibility of using the “tenant” context and then have the payload of the message in that subscription to contain the identification of the device so I can then process the payload for that particular device?  
I hope that makes sense.  
Thx

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [May 20, 2024, 4:49pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/4 "2024-05-20T16:49:11Z")

</div>

Currently there is no tenant context for measurments. However, to avoid missunderstandings here: just because you’ve created e.g. 1000 subscriptions in MO-context does not mean you’ll have to maintain 1000 consumers in your Microservice.  
You can bundle these MO-subscriptions in a single subscription and then have a single consumer that receives the messages of all Devices centrally. This is controlled by using the same subscription-name:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/5/0/50450426acf29c8f6d24450e3a68b84bc46b66af.png)

Note that the subscriptions need to be created only once, not regularly. The received notification will all have the source-id so that you know from which device they come from.

Optionally (e.g. in case the throughput is too high for your single consumer) you can make use of _shared consumers_. Then the messages are distributed to as many shared consumers as you’ve created.

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/5/2/52a612cc47f3dbe18e6067fbec5fba4ef629bc0e.png)

---

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 20, 2024, 5:36pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/5 "2024-05-20T17:36:56Z")

</div>

Good to know.  
This will work beautifully if the set of devices is fix. We do get new devices added when new customers are onboarded. Is there an elegant way of handling this special case?  
Thx

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [May 20, 2024, 6:19pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/6 "2024-05-20T18:19:27Z")

</div>

My idea would be that you mark each device that has a subscription with a fragment like

```json
{
   "c8y_IsDevice": {},
   "name": "...",
   "deviceSubscription": "eddies-subscription", 
   ...
}

```

And then you could have some job running in your services that queries from time to time “all devices without fragment ‘deviceSubscription’” - and add the subscription for any returned items plus add this fragment.  
The query for the search would be: `GET /inventory/managedObjects?query=$filter=has(c8y_IsDevice) and not has (deviceSubscription)`.

// Another option could be to setup a dedicated subscription for `managedobjects` in tenant-context. This subscription will receive a notification for all newly created Inventory objects. You would have to check if it is a Device and if yes, add it to your measurement subscription. Personally, I would choose the above option with the marker-fragment.

---

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 20, 2024, 6:53pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/7 "2024-05-20T18:53:49Z")

</div>

Sounds like a plan. I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![Reuben\_Miller2](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/reuben_miller2/32/317_2.png) [@Reuben\_Miller2](https://community.cumulocity.com/u/Reuben_Miller2)\
**Post date:** [May 20, 2024, 8:11pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/8 "2024-05-20T20:11:22Z")

</div>

Though just taking in step back…the notifications2 docs say that events are covered by the tenant scope…is if you really want the events created by ever device then that should work.

Though subscribing to all events may be a lot of data so you’d have to be sure there aren’t too many events being produced that the micro service is not capable of handling.

---

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 21, 2024, 10:29am UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/9 "2024-05-21T10:29:49Z")

</div>

Thanks Rueben. We have hundreds of devices all producing many events so the load on the microservice will definitely be tested. We will test out Korbinian’s first suggestion and will update.

Thanks All.

---

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 27, 2024, 12:27pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/10 "2024-05-27T12:27:07Z")

</div>

I tried the example in github suggested by Korbinian. I’m getting POST error working with an MO that specifically created and also one that we know exists. I’m sure it is something simple I’m overlooking. Following is what I did:

1. Using cumulocity python api, if I try to create a managed object:

```auto
`c8y = SimpleCumulocityApp()

mo_name = "abcdabcd99"
mo = ManagedObject(c8y, name=mo_name, type='c8y_CustomType').create()

```

I get this error:

```auto
ValueError: Unable to perform POST request. Status: 401 Response:
{"error":"security/Unauthorized","message":"Invalid credentials!","info":"https://cumulocity.com/guides/users-guide/getting-started/"}

```

1. If I try to create subscription for an already existing device managed object:

```auto
mo_id = "10416"
mo_name = "EF985D802901"
sub_name = f'{mo_name.replace("_", "")}Subscription'
sub = Subscription(c8y, name=sub_name, context=Subscription.Context.MANAGED_OBJECT, source_id=mo_id).create()

KeyError: 'No such object: /notification2/subscriptions'

```

Any ideas?  
Eddie

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [May 27, 2024, 1:23pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/11 "2024-05-27T13:23:35Z")

</div>

The SimpleCumulocityApp seems to be intended to run as a microservice only fetching the credentials from the environment variables. I think this is not working for your environment, that’s the reason you get 401 Unauthorized.  
You can try this

```auto
c8y = SimpleCumulocityApp()
print("CumulocityApp initialized.")
print(f"{c8y.base_url}, Tenant: {c8y.tenant_id}, User:{c8y.username}")

```

and check if the environment vars are correctly fetched. If not you could try to create a new instance by defining the required information manually like this

```auto
c8y = CumulocityApi(base_url=C8Y_BASEURL,
                    tenant_id=C8Y_TENANT,
                    username=C8Y_USER,
                    password=C8Y_PASSWORD)

```

Are you running Cumulocity in the cloud or Cumulocity Edge? Notification 2.0 is not available on all instances and also not on Cumulocity Edge.  
That might be the reason for your second error.

---

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 27, 2024, 1:44pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/12 "2024-05-27T13:44:50Z")

</div>

I’m running this in the Cloud.  
I think the notification 2.0 is not enabled. Let me check that first before trying your suggestion.  
Thx

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [May 27, 2024, 2:00pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/13 "2024-05-27T14:00:15Z")

</div>

There is also a dedicated `Notifications` permission that you’ll need to have:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/5/e/5e53abd8491b4c85666e687efdb660837ad114f4.png)

If you’re running your logic with a service-user you would also have to state `ROLE_NOTIFICATION_2_ADMIN` in your cumulocity.json.

Other than that, Stefan is right…Notification 2.0 is available on all our public cloud instances. For customer-specific instances you would have to check if its activated (there it is activated by request, not by default. See also [C8Y Notification 2: getting 404 error](https://community.cumulocity.com/t/c8y-notification-2-getting-404-error/7416)).

---

<div class="post-metadata">

**Author:** ![Kevin\_Palfreyman](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/kevin_palfreyman/32/29607_2.png) [@Kevin\_Palfreyman](https://community.cumulocity.com/u/Kevin_Palfreyman)\
**Post date:** [May 30, 2024, 4:46pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/14 "2024-05-30T16:46:15Z")

</div>

Might not be what you want, but have you also considered an alternate architecture where your Python logic is invoked as an in-process plug-in from an EPL App, and let Streaming Analytics consume the incoming data for you?

(I don’t think we currently have a sample or article showing exactly that, but we have such info for Python plug-ins, and separately for using the block SDK. Maybe someone else here has already done something similar)

---

<div class="post-metadata">

**Author:** ![edso](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@edso](https://community.cumulocity.com/u/edso)\
**Post date:** [May 31, 2024, 5:35pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/15 "2024-05-31T17:35:42Z")

</div>

Thanks Kevin.  
Yes we have but we want a purely microservice based approach.

We have finally gotten it to work. Thanks to everyone’s help. I’ll close this topic once we have more tests done.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [November 27, 2024, 5:36pm UTC](https://community.cumulocity.com/t/anyway-to-subscribe-to-s-us-topic-from-mqtt-client-to-receive-events-directly/2975/16 "2024-11-27T17:36:07Z")

</div>

Closed on November 27, 2024, 5:36pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
