Custom codec example - Avro
For example, republish a message with the “name” field as “Shawn” to the topic “avro_user/Shawn”.
In the Dashboard (opens new window) interface of EMQX, create an Avro Schema using the following parameters:
Name: avro_user
Schema:
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 “avro_user”;
- Action Type: Message republishing
- Destination Topic: avro_user/${avro_user.name}
- Message Content Template: ${avro_user}
This action sends the decoded “user” to the topic avro_user/${avro_user.name}
in JSON format. 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 User message and encode it as binary data, then sends it to the “t/1” topic. See for details.
- Check that a message with the topic
avro_user/Shawn
is received on the Websocket side: