emqx / emqx-web-hook

EMQX Webhook Plugin
https://www.emqx.com
Apache License 2.0
79 stars 36 forks source link

Is there an easy way to modify web_hook to allow changing message data? #118

Open damouse opened 5 years ago

damouse commented 5 years ago

I need to modify publishes in-flight from Python. I was hoping web_hook would let me do that, but then I saw this ticket that said that only lua can do that.

I plan to deploy the authentication, authorization, and modification service alongside the broker, and its very important for my application that I can manipulate those messages. In the issue above someone says its possible to modify the web_hook extension to modify data.

I don't know erlang. I could learn it, but it would take some time. Is there a simple example for how to modify the web_hook extension to modify the payload on a publish based on the results of the http call? I understand if this isn't a trivial task, thanks for your time.

HJianBo commented 5 years ago

Yes, you can't update any content of a publish message by the official emqx_web_hook.

So, if want using web_hook to implement this, maybe you should receive the HTTP response to update the message content in the on_message_publish callback function emqx_web_hook.erl#L189.

i.e:

on_message_publish(Message = #message{topic = Topic, flags = #{retain := Retain}}, {Filter}) ->
    with_filter(
      fun() ->
        {FromClientId, FromUsername} = format_from(Message),
        Params = [{action, message_publish},
                  {from_client_id, FromClientId},
                  {from_username, FromUsername},
                  {topic, Message#message.topic},
                  {qos, Message#message.qos},
                  {retain, Retain},
                  {payload, Message#message.payload},
                  {ts, emqx_time:now_secs(Message#message.timestamp)}],
        %% Modify these lines:
        %% send_http_request(Params),
        %% {ok, Message}
        %% To:
        Response = send_http_request(Params),
        NewMessage = parse_http_resp_to_message(Response, Message),
        {ok, NewMessage}
      end, Message, Topic, Filter).

But, you should understand the Hooks Mechnism and Erlang first!

slaesh commented 3 years ago

Still not possible? @HJianBo

This request was old, maybe in the meantime things changed :)

I saw this here: https://www.emqx.com/en/blog/develop-emqx-plugin-using-python using the hooks-api..

Use-Case: appending a timestamp to JSON-payloads.

thanks! :)

slaesh commented 3 years ago

nvm, found this here: https://github.com/emqx/emqx-extension-examples/tree/master/exhook-svr-go

works fine! thanks!