# Counting messages per main topic

**URL:** <https://discourse.nodered.org/t/counting-messages-per-main-topic/56814>\
**Category:** General\
**Created:** [17 January 2022 16:47 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814 "2022-01-17T16:47:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)\
**Post date:** [17 January 2022 16:47 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814/1 "2022-01-17T16:47:32Z")

</div>

Hi,

Looking for advice/solution for the following problem:

A got an mqtt broker with lots of topics and subtopics.  
I wish to get a list of main topics, and check how many messages are published per each main topic, including ist subtopics.  
Let's say this many messages were published to the broker:

Maintopic1/Subtopic1 - 2 messages  
Maintopic1/Subtopic1/Subtopic - 2 messages  
Maintopic1/Subtopic2 - 2 messages  
Maintopic2 - 1 message  
Maintopic2/Subtopic1 - 1 message

The result I would like to get automatically would look like this:  
Maintopic1 - 6 messages  
Maintopic2 - 2 messages

I have used 'node-red-contrib-msg-speed' to check how many messages are being published in a certain topic. That's basically what I want, but without have to give the topic. It should check all main topics.

Sorry if my explanation is not easy to understand....

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 January 2022 17:07 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814/2 "2022-01-17T17:07:47Z")

</div>

First you would need to split the topic and grab the first part.

Then you could use a function node and an object stored in context to count them...

### example...

![chrome_ay2VTcASfH](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/6742a9f9475a68d3916cc1c8a8809fd07ed5693e.gif)

### demo flow...

```auto
[{"id":"8f7f9a59772e9c54","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main1/sub1","payload":"","payloadType":"date","x":1940,"y":800,"wires":[["4b61f945350d10d6"]]},{"id":"4b61f945350d10d6","type":"function","z":"a4633311d1bf470d","name":"topic counter","func":"let topic_counter = flow.get(\"topic_counter\") || {};\n\nif(msg.topic == \"CLEAR-STORE\") {\n topic_counter = {};\n} else {\n const main = msg.topic.split(\"/\")[0]\n if(topic_counter[main] == null) {\n topic_counter[main] = 0;\n }\n topic_counter[main]++;\n}\n\nflow.set(\"topic_counter\", topic_counter);\n\nmsg.payload = topic_counter;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2190,"y":860,"wires":[["159f9dcabd276f59"]]},{"id":"e6ce8b5ac7c69af1","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main1/sub2","payload":"","payloadType":"date","x":1940,"y":840,"wires":[["4b61f945350d10d6"]]},{"id":"43647647ced4c82c","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main2/sub1","payload":"","payloadType":"date","x":1940,"y":880,"wires":[["4b61f945350d10d6"]]},{"id":"34fb52f7e712aa72","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main21/sub2","payload":"","payloadType":"date","x":1940,"y":920,"wires":[["4b61f945350d10d6"]]},{"id":"159f9dcabd276f59","type":"debug","z":"a4633311d1bf470d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2370,"y":860,"wires":[]},{"id":"db507ead0e26e1af","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"CLEAR-STORE","x":1960,"y":740,"wires":[["4b61f945350d10d6"]]}]

```

---

<div class="post-metadata">

**Author:** ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)\
**Post date:** [17 January 2022 17:37 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814/3 "2022-01-17T17:37:00Z")

</div>

Hi Steve-Mci,

I am a beginner and impressed by your skills, to put out a solution just like that !  
Works perfectly, giving a broker subscription with wildcard (#) to the input, the output gave me the list of main topics, and the total count of all messages within. Numbers constantly increasing of course, until variables are cleared.

I guess, to be able to do use the speed node (measuring how many messages were published withing given time), I would need to sort all messages per main topic ?

Can a function node have more outputs? (Like a switch node?)  
All messages to maintopic1/# - output1, all messages to maintopic2/# to output 2, and so on ...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 January 2022 17:40 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814/4 "2022-01-17T17:40:56Z")

</div>

> [@mzsolnay](#):
>
> Can a function node have more outputs? (Like a switch node?)

Yes: [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#multiple-outputs)

> [@mzsolnay](#):
>
> I guess, to be able to do use the speed node (measuring how many messages were published withing given time), I would need to sort all messages per main topic ?

I dont know that node. Probably best to start a new thread, stating this node name in the topic so that it catches the attention of someone who knows it can assist.

PS: Dont forget to mark the post you believe to be the solution.

* * *

> [@mzsolnay](#):
>
> I am a beginner and impressed by your skills

As I do to all beginners, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)\
**Post date:** [17 January 2022 17:48 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814/5 "2022-01-17T17:48:06Z")

</div>

Thank you very much for your help !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [31 January 2022 17:48 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814/6 "2022-01-31T17:48:13Z")

</div>

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