3rd-Party Custom codec example - gRPC

    Create a resource Parser gRPC

    In the EMQX Dashboard 的 Resource 创建 (opens new window) interface of EMQX, create a Parser gRPC resource using the following parameters:

    • URL:
    • Resource ID: my_grpc_parser_resource

    In the interface of EMQX, create a 3rd-Party Schema using the following parameters:

    1. Name: my_grpc_parser
    2. Codec Type: 3rd-party
    3. Third Party Type: Resources

    All other configurations remain default.

    Creating rules

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

    This SQL statement first encodes and then decodes the data to verify that the encoding and decoding process is correct:

    • The function encodes the contents of the payload field according to the Schema ‘my_grpc_parser’ and stores the result in the variable encode_resp;
    • The schema_decode function decodes the contents of the payload field according to the Schema ‘my_grpc_parser’ and stores the result in the variable ;

    Then add the action using the following parameters:

    • Action Type: Check (debug)

    This check action prints the results filtered by the SQL statement to the emqx console (erlang shell).

    If the service is started with emqx console, the print will be displayed directly in the console; if the service is started with emqx start, the print will be output to the erlang.log.N file in the log directory, where “N” is an integer, e.g. “erlang.log.1”, “ erlang.log.2”.

    Once the rules have been created, it is time to simulate the data for testing. Therefore, the first thing you need to do is write your own codec service.

    The following code implements an gRPC codec service using the Python language. For simplicity, this service just do base64_encode on received string when encoding, and do base64_decode when decoding. See full code (opens new window) for details.

    Checking rule execution results

    Since this example is relatively simple, we’ll use the MQTT Websocket client directly to simulate sending a message on the device side.

    1. In the Dashboard’s Websocket3rd-Party Custom codec example - gRPC - 图4 (opens new window) tools, log in to an MQTT Client and publish a message to “t/1” with the text “hello”.

    2. Check what is printed in the emqx console (erlang shell):

    Select Data is the data filtered by the SQL statement, Envs are available environment variables within the rule engine and Action Init Params is the initialization parameters for actions. All three data are in format.

    The two fields decode_resp and encode_resp in Selected Data correspond to the two ASs in the SELECT statement.