benthos + javascript = <3
# general
j
Guys, did you know you could use javascript in benthos instead of bloblang? you can now really easy work with chatgpt, just give this example code and tell it what the input is and it will generate it. it even has console.log statements πŸ˜‚
Copy code
yaml

pipeline:
  processors:
    - javascript:
        code: |
          (() => {
            let payload = benthos.v0_msg_as_structured();
            console.log("begin");
            // Step 1: Sort the array by the 'name' field numerically
            payload.sort(function(a, b) {
              return parseInt(a.name, 10) - parseInt(b.name, 10);
            });
            // Step 2: Convert each 'value' to its corresponding ASCII character
            var chars = [];
            for (var i = 0; i < payload.length; i++) {
              chars.push(String.fromCharCode(payload[i].value));
            }
            console.log("end")
            
            // Step 3: Join the characters into a single string
            var resultString = chars.join('');
            
            // Step 4: Assign the resulting string to a new field, e.g., 'converted_string'
            let newPayload = {}
            newPayload.converted_string = resultString;
            benthos.v0_msg_set_structured(newPayload);
          })();
https://cdn.discordapp.com/attachments/984082664678125578/1314635254962720900/image.png?ex=67547cfd&is=67532b7d&hm=52d9d35cf294c2ddfb11c2e8f022e7ff7f6e8a5097a7279f5e90a684af6e3afa&
This is the boilerplate template for chatgpt: I have this input: [your input] I want this: [describe your output] Please keep the boilerplate code as it is and only change where the console.log() statement is. Please be ECMAScript 5.1 compliant.
Copy code
pipeline:
  processors:
    - javascript:
        code: |
          (() => {
            let payload = benthos.v0_msg_as_structured();

            console.log("add here your custom code")

            benthos.v0_msg_set_structured(newPayload);
          })();
@Diederik @DanielH @Brian Pribe @trentc @Denis @Luke van Enkhuizen
b
So I love this feature. I think the workflow of going from nodered just to make it work to benthos for a production grade pipeline is killer.
l
Yes, definitely so many new possibilties here! I'm just starting to scratch the surface of what's possible. Are you guys also using Benthos Studio to visualize things before hand or even deploy?
j
we are using the playground for bloblang. and before we started with all the bugfixes we were putting such a testing logic in the management console as well, but we needed to stop it to focus on reliability. now I am kind of lucky that we did, because over the last weeks we gained more insights like the benthos playground, which is just a wasm thing, so you could simulate the entire benthos processing behaviour in the frontend, which would make it really fast, and in my opinion even more intuitive than Node-RED. but still a long way to go. hopefully Q1 2025.
d
Yes. This was shared in the chat quite some time ago. Not sure who shared it. It is quite nice, but i don't want to use to much JavaScript in the backend since it is really resource heavy. Not sure how it compares to bloblang. But this is also another reason to have a custom processor made in golang to handle the data. It will consume much less resources. Anyway js support in benthos will make it more accessible for others.
d
omfg
umh should migrate from bloblang to javascript i experience bloblang as a barrier (chatgpt doesnt like bloblang :p)
j
Need to investigate performance better for the bridges, but for protocol converter and DFCs agreed! I only banged my head against my table 6 times yesterday evening when I found it out. My colleagues (plural) where sitting for a day straight to solve some data processing errors in blinking, when o1-mini solved it on the first try within 20 seconds with JavaScript
b
Bloblang can do some really crazy digging and transforming, but the experience isn’t there like JavaScript.
d
i really like JavaScript for the simplicity but for performance it is a nightmare.
i would believe that bloblang is way faster and neeb much less resources since it written i golang.
d
I'm personally sticking with bloblang (partly because I don't know javascript)
d
i think that's a very nice way to implement it
will it have all the same functionallity as a js function has in nodered?
publish to different topics as node red has,...
j
It will not have the same feature set as node red, but I think it will actually help. In my experience, some node red features (e.g. global variables) lead in practice to messy node red flows. What do you mean with different topics?