@@ -407,6 +407,68 @@ the invocation of your function, you will create an endless loop.
407407:::
408408
409409
410+ (user-defined-functions-languages)=
411+ ### Using other languages
412+
413+ This section is about using languages other than Python within the mqttwarn transformation
414+ subsystem.
415+
416+ #### Introduction
417+ Imagine you are running a large cloud infrastructure for [ 4IR] purposes, where hardware
418+ appliances or industrial machines submit telemetry data to concentrators or hubs, in order
419+ to converge them to a message bus like [ Kafka] , where, because you favoured a [ FaaS]
420+ solution, your [ serverless] function handler will consume the telemetry messages, in
421+ order to conduct further processing, like to filter, transform, and store the data.
422+ Chances are that you used the [ Serverless Framework] , built on [ Node.js] , so your application
423+ infrastructure is effectively based on a bunch of [ JavaScript] code.
424+
425+ Now, you know how the story ends. The infrastructure costs exploded, you had to fire your
426+ engineering team, and now you are sitting in front of a mixture of infrastructure setup
427+ recipes and actual application code, and you are not able to make any sense of that at all.
428+ After some digging, you are able to identify the spot where the real application logic
429+ evaluates a set of transformation rules, in order to mangle inbound data appropriately
430+ into outbound data, written in JavaScript.
431+
432+ You quickly realize that the same thing could effectively be solved using [ MQTT] and Python,
433+ and operated on a single server machine, because you are doing only a few hundred requests
434+ per second anyway. However, you don't have the time, because, well, the customer quickly
435+ needs a fix because they added another telemetry data field waiting to be consumed and
436+ handled appropriately. Well, and it's Kafka anyway, how would one get rid of _ that_ ?
437+
438+ After a glimpse of desperation, you are reading the fine manual again, and catch up with
439+ the fact that telemetry data _ is actually ingested using MQTT!_ That is useful, indeed!
440+ Remembering conversations about a thing called _ mqttwarn_ at the Späti the other day,
441+ and that it gained the ability to run JavaScript code recently, in order to bring cloud
442+ computing techniques back to personal computing, you think it would be feasible to change
443+ that architecture of your system in the blink of an eye.
444+
445+ There you go: You rip out the JavaScript transformation rules into a single-file version,
446+ export its main entry point symbol, configure mqttwarn to use ` functions = mycloud.js ` ,
447+ and adjust its settings to use your MQTT broker endpoint at the beginning of the data
448+ pipeline, invoke mqttwarn, and turn off Kafka. It works!
449+
450+ :::{note}
451+ Rest assured we are overexaggerating a bit, and [ Kafka] can only be compared to [ MQTT]
452+ if you are also willing to compare apples with oranges, but you will get the point that
453+ we believe simpler systems are more often than not able to solve problems equally well,
454+ if not more efficient, both at runtime, and on details of maintenance and operation.
455+
456+ Other than this, every kind of system migration should be conducted with better planning
457+ than outlined in our rant above.
458+ :::
459+
460+ #### JavaScript
461+
462+ For running user-defined functions code written in [ JavaScript] , mqttwarn uses the
463+ excellent [ JSPyBridge] package. For adding JavaScript support to mqttwarn, install
464+ it using pip like ` pip install --upgrade 'mqttwarn[javascript]' ` , or use one of the
465+ available [ OCI images] ( #using-oci-image ) .
466+
467+ You can find an example implementation for a ` filter ` function written in JavaScript
468+ at the [ OwnTracks-to-ntfy example tutorial] ( #owntracks-ntfy-variants-udf ) .
469+
470+
471+
410472## User-defined function examples
411473
412474In this section, you can explore a few example scenarios where user-defined
@@ -507,6 +569,12 @@ was received on, here `owntracks/jane/phone`. The name of the section will be
507569the ` section ` argument, here ` owntracks/#/phone ` .
508570:::
509571
572+ :::{note}
573+ The recipe about how to use [ ] ( #owntracks-ntfy-variants-udf ) demonstrates
574+ corresponding examples for writing a ` filter ` function.
575+ :::
576+
577+
510578(decode-topic)=
511579### ` alldata ` : Decoding topic names
512580
@@ -631,10 +699,19 @@ weather,topic=tasmota/temp/ds/1 temperature=19.7 1517525319000
631699```
632700
633701
702+ [ 4IR ] : https://en.wikipedia.org/wiki/Fourth_Industrial_Revolution
703+ [ FaaS ] : https://en.wikipedia.org/wiki/Cloud_computing#Serverless_computing_or_Function-as-a-Service_(FaaS)
634704[ geo-fence ] : https://en.wikipedia.org/wiki/Geo-fence
635705[ InfluxDB line format ] : https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_tutorial/
706+ [ JavaScript ] : https://en.wikipedia.org/wiki/JavaScript
636707[ Jinja2 templates ] : https://jinja.palletsprojects.com/templates/
708+ [ JSPyBridge ] : https://pypi.org/project/javascript/
709+ [ Kafka ] : https://en.wikipedia.org/wiki/Apache_Kafka
710+ [ MQTT ] : https://en.wikipedia.org/wiki/MQTT
711+ [ Node.js ] : https://en.wikipedia.org/wiki/Node.js
712+ [ OwnTracks ] : https://owntracks.org
637713[ Tasmota ] : https://github.com/arendst/Tasmota
638714[ Tasmota JSON status response for a DS18B20 sensor ] : https://tasmota.github.io/docs/JSON-Status-Responses/#ds18b20
639- [ OwnTracks ] : https://owntracks.org
715+ [ serverless ] : https://en.wikipedia.org/wiki/Serverless_computing
716+ [ Serverless Framework ] : https://github.com/serverless/serverless
640717[ waypoint ] : https://en.wikipedia.org/wiki/Waypoint
0 commit comments