Global Lua variable resetting in HTTP API call within Prosody module

26 Views Asked by At

I'm encountering a peculiar issue while developing a Prosody module for a Jitsi server. The module is designed to increment a global sum variable by 5 for every participant that joins or leaves a conference. This incrementation works as expected within the muc-occupant-joined and muc-occupant-left event hooks. However, the value of sum appears to reset or fails to persist when accessed through a custom HTTP GET API endpoint, always returning 0.

local json = require "dkjson"
local basexx = require "basexx"

sum = 0

function participant_joined(event)
    sum = sum + 5
end

function participant_left(event)
    sum = sum + 5
end

module:hook("muc-occupant-joined", participant_joined)
module:hook("muc-occupant-left", participant_left)

function get_total_sum(event)
    return { status_code = 200; body = json.encode({totalSum = sum}); };
end

function module.load()
    module:depends("http");
    module:provides("http", {
        default_path = "/";
        route = {
            ["GET sum"] = get_total_sum;
        };
    });
end

I'm seeking insights or suggestions on why the sum would not retain its value for the HTTP endpoint calls in this specific module context.

0

There are 0 best solutions below