Skip to content

Instantly share code, notes, and snippets.

@thalesmg
Created May 22, 2023 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thalesmg/3c5fdbae2843d63c2380886e69d6123c to your computer and use it in GitHub Desktop.
Save thalesmg/3c5fdbae2843d63c2380886e69d6123c to your computer and use it in GitHub Desktop.
Example of how to use Schema Registry + Rule Engine in EMQX 5.0 (protobuf)
// Generate `person_pb2.py` by running `protoc --python_out=. person.proto` before
// running `protobuf_mqtt.py`
syntax = "proto2";
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
# Original code: https://github.com/terry-xiaoyu/schema-registry-examples/blob/master/protobuf/pb2_mqtt.py
import paho.mqtt.client as mqtt
import io
import person_pb2
import time
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, obj, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("t/#")
time.sleep(0.5)
publish_msg(client)
client.unsubscribe("t/#")
def publish_msg(client):
p = person_pb2.Person()
p.id = 1
p.name = "Shawn"
p.email = "shawn@example.com"
message = p.SerializeToString()
topic = "t/1"
print("publish to topic: t/1, payload:", message)
client.publish(topic, payload=message, qos=0, retain=False)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client(client_id = "protobuf")
client.reconnect_delay_set(min_delay=120, max_delay=121)
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 300)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment