Custom codec example - Protobuf

    For example, republish a message with the “name” field as “Shawn” to the topic “person/Shawn”.

    In the Dashboard (opens new window) interface of EMQX, create a Protobuf Schema using the following parameters:

    1. Schema: The following protobuf schema defines a Person message.

    Use the Schema you have just created to write the rule SQL statement:

    The key point here is :

    • The schema_decode function decodes the contents of the payload field according to the Schema ‘protobuf_person’;
    • stores the decoded value in the variable “person”;
    • The last argument Person specifies that the message type in the payload is the ‘Person’ type defined in the protobuf schema.
    • Destination Topic: person/${person.name}
    • Message Content Template: ${person}

    This action sends the decoded “person” to the topic in JSON format. ${person.name} is a variable placeholder that will be replaced at runtime with the value of the “name” field in the message content.

    Once the rules have been created, it is time to simulate the data for testing.

    The following code uses the Python language to fill a Person message and encode it as binary data, then sends it to the “t/1” topic. See for details.

    1. Install the python dependency and execute the device-side code:

    1. Check that a message with the topic is received on the Websocket side: