# UBJSON extension

**URL:** https://discourse.nodered.org/t/ubjson-extension/78082
**Category:** Industrial
**Created:** [1 May 2023 21:03 UTC](https://discourse.nodered.org/t/ubjson-extension/78082 "2023-05-01T21:03:11Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [1 May 2023 21:03 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/1 "2023-05-01T21:03:11Z")

</div>

I'm using MQTT to get some data from PLC by robust way. MQTT protocol is light way enough itself, but data preparation consumes huge resources of PLC. JS didn't supports PlC's data types at all. I.e. Simatic S7 and JS have not any common data types except of string. Because of that I have to form JSON-string on PLC's side.  
I looked around the issue and find out the UBJSON extension of JSON can help me. But I can't understand how add UBSJON to Node-RED.  
So, any suggestions are kindly appreciated.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [2 May 2023 07:50 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/2 "2023-05-02T07:50:03Z")

</div>

Welcome to the forum @zova

Can you give an example of data types that are not supported?  
Please also explain your system architecture. Is the PLC connecting directly to MQTT?

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [2 May 2023 13:05 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/3 "2023-05-02T13:05:39Z")

</div>

My architecture is PLC -\> MQTT -\> Node-RED.  
Only one way robust transmission nedeed. Reverse direction transmission uses Hilscher S7 Node-RED driver.  
For PLC -\> MQTT -\> Node-RED all data types are unsupported except String.  
Yes, my PLC is connected directly to MQTT.

It works, but consumes huge resources on PLC side for preparing JSON string.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 May 2023 13:13 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/4 "2023-05-02T13:13:46Z")

</div>

> data preparation consumes huge resources of PLC

> It works, but consumes huge resources on PLC side for preparing JSON string.

I _think_ you are trying to say that it consumes huge resources on the PLC preparing a JSON string.

But the question is, why prepare a JSON string on the PLC?  
JSON formatted data is nice to have but it's not mandatory.  
Why can't you send whatever the PLC has and consume (more plentiful) resources on the Node-red machine?

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [2 May 2023 19:17 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/5 "2023-05-02T19:17:47Z")

</div>

> [@](#):
>
> I _think_ you are trying to say that it consumes huge resources on the PLC preparing a JSON string.

As You can see, english is not my native language, however You got right main thought. Thanks.  
It's not so much the JSON itself, but converting number to string on PLC side.  
I think UBJSON will help me escapes the convertion.

> [@](#):
>
> But the question is, why prepare a JSON string on the PLC? Why can't you send whatever the PLC has and consume (more plentiful) resources on the Node-red machine?

So, what is your suggestion?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 May 2023 19:31 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/6 "2023-05-02T19:31:29Z")

</div>

Show us an example of the data which your PLC can easily send to Node-red, and the format you want it to be in.

No doubt someone will be able to construct a flow to convert one to the other.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [2 May 2023 19:48 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/7 "2023-05-02T19:48:07Z")

</div>

Note, if it helps, that mqtt will accept a binary buffer and node red has nodes that can interpret the data in a buffer.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [2 May 2023 20:12 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/8 "2023-05-02T20:12:58Z")

</div>

There is a [ubjson npm module](https://www.npmjs.com/package/@shelacek/ubjson) you could use

Add `@shelacek/ubjson` in the "setup" tab then in the "on message" tab, something like this should do it...

```auto
msg.payload = ubjson.decode(msg.payload)
return msg

```

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [2 May 2023 20:49 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/9 "2023-05-02T20:49:21Z")

</div>

I already installed that npm and works with it. Not all are clear but it is just beginning.

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [6 May 2023 10:04 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/10 "2023-05-06T10:04:20Z")

</div>

npm had been installed without any error but Node-Red didn't recognize it. What is wrong?

 ![install screenshot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/7152f25f4fc935d2a20ed1537789eae3dd760f7c.jpeg)  
 ![not ok](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc1590631a371cd345e4e2ade12d020a61f6094c.png)

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [6 May 2023 12:44 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/11 "2023-05-06T12:44:35Z")

</div>

I realized that npm should been installed directly from Node-RED. By default in my Node-RED installation external npm modules was switched off. Also here wasn't settings.js file. I downloaded settings.js from Node-RED site, placed it to .node-red folder, edit and reboot - the button allow loading external npm appears. But now I can't find out the tgz packed ubjson npm. Ok, it possible to create tgz packed npm using package.json file from npm download directory... I need a break.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 May 2023 13:52 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/12 "2023-05-06T13:52:35Z")

</div>

> [@Steve-Mcl](#):
>
> Add `@shelacek/ubjson` in the "setup" tab

Read again ☝

 ![Screenshot_20230506-145258](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/99fe54d368cb2443c43eb7d8f1074b514cc6c7e6.png)

You simply enter that on the **setup** tab on the function node. Then use it (without the require)

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [6 May 2023 14:03 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/13 "2023-05-06T14:03:15Z")

</div>

Yes. I did that. And I got "myHomeTemperatures/50875 : msg.payload : undefined" error.

 ![setup](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/44aa646095cf4f55af14f9bff0be17e63144204a.png)  
 ![func](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d9f6ecfd841cf2ac150ea86fa46fcb6cfdd7bab3.png)

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [6 May 2023 14:06 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/14 "2023-05-06T14:06:38Z")

</div>

Fortunately it's simple enough to made the tgz, here:  
npm pack @shelacek/ubjson

After that I got "shelacek-ubjson-1.1.1.tgz".

When I try load the "shelacek-ubjson-1.1.1.tgz" from Node-RED I got error "Module not found". Remote or local I do it it doesn't matter the same result.

I'm in deadlock.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 May 2023 14:19 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/15 "2023-05-06T14:19:37Z")

</div>

Change the MQTT node to deliver a buffer.

There is no need to make a tgz, adding it to the function node is all you need to do.

Ps. Uploading TGZ modules is for Node-RED node packages - I.e. node-red-contrib-xyz. This module is simply a library - not a full on Node-RED node package - so that won't work here

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [6 May 2023 20:11 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/16 "2023-05-06T20:11:12Z")

</div>

Thanks, it works!

---

<div class="post-metadata">

### Author: ![zova](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zova/32/79487_2.png) [@zova](https://discourse.nodered.org/u/zova)
#### Post date: [7 May 2023 14:51 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/18 "2023-05-07T14:51:31Z")

</div>

> [@](#):
>
> Why can't you send whatever the PLC has and consume (more plentiful) resources on the Node-red machine?

So, I tested all ways: JSON, UBJSON and finally binary. (Simatic -?-\> MQTT -?-\> Node-RED -\> MySQL)  
As for me, the most graceful is binary way with help of node-red-contrib-buffer-parser.  
Thanks!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [21 May 2023 14:52 UTC](https://discourse.nodered.org/t/ubjson-extension/78082/19 "2023-05-21T14:52:01Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
