pythonmqttgoogle-cloud-pubsubtelemetrygoogle-cloud-iot

Can we set custom message attribute publishing to pubsub over the mqtt bridge


I am using MQTT to send and receive messages from/to Google IoT Core (telemetry messages). Messages are then fowarded to different Pub/Sub Topic.

I'd like to add custom attributes to my messages.
The goal is to use differents subscriptions on my topic. Then, filter incoming messages by my custom attributes, and finally get my messages by 'theme' on my dataflow pipeline.
I know we can do it when we use the Pub/Sub client but I can't manage to do this from the mqtt client.
I am using the python client.


So as it was requested I add some details. This is a very classic situation. I am using a MQTT client as in the google example here: https://cloud.google.com/iot/docs/how-tos/mqtt-bridge#configuring_mqtt_clients. Then using the publish code from the same documentation: https://cloud.google.com/iot/docs/how-tos/mqtt-bridge#publishing_telemetry_events (I juste replaced

   for i in range(0, 60):    
        time.sleep(1)
        client.loop()

by time.sleep(1) because I do not want to wait a minute between each message.

I publish my messages calling the previous code like this:
publisher.publish(topic, payload)
where topic is my PubSub topic and payload is my data.

The documentation says:

The forwarded message data field contains a copy of the message published by the device, and the following message attributes are added to each message in the Cloud Pub/Sub topic

(link if you want to see the attributes: https://cloud.google.com/iot/docs/how-tos/mqtt-bridge#publishing_telemetry_events) What I want to do is add custom attributes to this list. If I call the Pub/Sub client directly I can do this (from documentation):

    # Add two attributes, origin and username, to the message
    future = publisher.publish(
       topic_path, data, origin="python-sample", username="gcp"
     )

where origin and username are custom attributes, Is it possible to do this using the MQTT client ?

Thanks :)


Solution

  • So it is NOT possible. What google advice is to use topic sub-directories to 'group' messages by theme.

    You can associate topics to your registry, then set subfolder for your topic. You can have as much subdirectory as you want as long that it follows this convention:

    topic: topic-name
    subtopic: topic_name/theme1, topic_name/theme2....