Hooking publish and adding User-Properties or other data?

9 views
Skip to first unread message

Daniel Santos

unread,
Jun 6, 2025, 4:21:49 PMJun 6
to vernemq-users
Hello,

I'm new to vernemq and MQTT 5. I'm using vernemq as a broker for mobile units to talk to server processes and trying to minimize traffic for the mobile users. I essentially need the server processes to receive the authid or client id (if authenticated) for each message they receive. I've been experimenting and while my hook is being called, I can't get any changes to the publication alter what my server process receives.

BTW, also not a Lua pro.

function my_on_deliver(pub)
    print("on_publish hook triggered for topic: " .. pub.topic)
    log.info("======== on_publish " .. pub.topic .. " ========")

    if not pub.properties then
        pub.properties = {}
    end
    log.info("pub.properties.p_response_topic: " .. (pub.properties.p_response_topic or "(none)"))
    pub.properties.p_response_topic = "asdf"
    if not pub.properties.p_user_property then
        pub.properties.p_user_property = {}
    end
    log.info("pub.properties.p_user_property['sender']: " .. (pub.properties.p_user_property["sender"] or "(none)"))
    pub.properties.p_user_property["hello"] = "o"
    return true
end

hooks = {
    on_deliver_m5 = my_on_deliver,
}


Does this hook not support any changes? Ideally, I would like to have mobile clients auth, and if they get a new session to add some data to it that the server-side clients can access or receive upon publish.

Thank you!
Daniel

André Fatton

unread,
Jun 7, 2025, 3:40:37 AMJun 7
to vernemq-users
Hello Daniel,

while you could fully reshape a message payload with the auth_on_publish hook, you probably want to use the v5 properties to forward a "sender" value directly set by the publisher. PUB/SUB is decoupling the receiver from the publisher. If we need the receiver to know the publisher, I think it's best to solve this on the application protocol layer. (which means the publisher adds that information).
I don't see how this would minimize traffic, though.

Cheers,
André
Reply all
Reply to author
Forward
0 new messages