Hi!
I'm working on an MQTT reverse proxy.
In the js_preread handler function I can do this:
function preread(s) {
s.on("upload", function(data, flags) {
ngx.fetch("http://...").then(reply => reply.text().then(result => {
if (isResultCool(result)) {
s.allow();
} else {
s.deny();
}
}));
}
}
and it works (so the connection gets allowed or denied based on the async result of my fetch call).
But in the js_filter handler function, this doesn't work:
const DISCONNECT = Uint8Array.of(224, 0);
function filter(s) {
s.on("upload", function (data, flags) {
ngx.fetch("http://...").then(reply => reply.text().then(result => {
if (isResultCool(result)) {
s.send(data);
} else {
s.send(DISCONNECT);
}
}));
});
}
So basically s.send(...) doesn't seem to work if I call it in the callback of an asynchronous function.
My questions are:
1. Is this intentional, or a bug?
2. If it's intentional, is there any way to make a blocking http call in the js_filter handler method?
This is very frustrating because there is some information I only have in the MQTT SUBSCRIBE an PUBLISH messages (which are only passed to the filter function), so I cannot use the info in the CONNECT message to make this call to the HTTP endpoint.
Thanks in advance.
(Sorry for the formatting, I don't know how to insert code blocks here.)