# SAMFD with WebUI?

**URL:** <https://iplug2.discourse.group/t/samfd-with-webui/528>\
**Category:** Web Audio Modules\
**Created:** [25 December 2021 21:06 UTC](https://iplug2.discourse.group/t/samfd-with-webui/528 "2021-12-25T21:06:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![telefonica](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/telefonica/32/224_2.png) [@telefonica](https://iplug2.discourse.group/u/telefonica)\
**Post date:** [25 December 2021 21:06 UTC](https://iplug2.discourse.group/t/samfd-with-webui/528/1 "2021-12-25T21:06:02Z")

</div>

I’ve been playing around with iPlug2 and the WebUI and I love it so far!

I’ve been able to send messages from the UI to the module using SAMFUI, etc. However, I’m trying to wrap my head around receiving messages and can’t find any documentation or examples.

Here’s what I’ve tried so far:

// ts

```javascript
export const Elem = () => {
  useEffect(() => {
    window.SAMFD = (msgTag: number, dataSize: number, msg: Array<number>) => {
      // var decodedData = window.atob(msg);
      console.log("SAMFD msgTag:" + msgTag + " msg:" + msg);
    }
  }, [])

  return (
    <Button
      onClick={() => {
        if (NODE_ENV === 'production') {
          SAMFUI(2, -1, '');
        }
      }}
    >
      Ping
    </Button>
  )
}

```

// MyModule.cpp

```auto
const uint8_t pData = 7;
const uint8_t* pDataPtr = &pData;

bool MyModule::OnMessage(int msgTag, int ctrlTag, int dataSize, const void* pData)
{
  DBGMSG("have mesage; tag %d\n", msgTag);
  switch (msgTag) {
  case 2:
  {
    MyModule::SendArbitraryMsgFromDelegate(0, 8, pDataPtr);
  }
  default:
  {
    DBGMSG("unknown mesage tag %d\n", msgTag);
  }
  }

  return false;
}

```

However, I get a heap corruption error. What have I done wrong? Thanks!

 ![image](https://global.discourse-cdn.com/free1/uploads/iplug2/original/1X/a899d8ea3c257a6af95511ca913fd7aaa9dd5d90.png)

---

<div class="post-metadata">

**Author:** ![olilarkin](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/olilarkin/32/2_2.png) [@olilarkin](https://iplug2.discourse.group/u/olilarkin)\
**Post date:** [26 December 2021 10:29 UTC](https://iplug2.discourse.group/t/samfd-with-webui/528/2 "2021-12-26T10:29:12Z")

</div>

Are you trying to send a message back to the UI when you receive msgTag with ID2?

The data pointed to by pDataPtr is 1 char / 1byte not 8 bytes, so the 2nd argument to SendArbitraryMsgFromDelegate() is wrong. That might be what’s crashing it

---

<div class="post-metadata">

**Author:** ![telefonica](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/telefonica/32/224_2.png) [@telefonica](https://iplug2.discourse.group/u/telefonica)\
**Post date:** [26 December 2021 18:21 UTC](https://iplug2.discourse.group/t/samfd-with-webui/528/3 "2021-12-26T18:21:28Z")

</div>

> [@olilarkin](#):
>
> Are you trying to send a message back to the UI when you receive msgTag with ID2?

Yup!

> [@olilarkin](#):
>
> The data pointed to by pDataPtr is 1 char / 1byte not 8 bytes, so the 2nd argument to SendArbitraryMsgFromDelegate() is wrong. That might be what’s crashing it

Hmmm… thanks for the suggestion. I changed the second argument to `1` and the crash persists. I also tried `sizeof`.

---

<div class="post-metadata">

**Author:** ![telefonica](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/telefonica/32/224_2.png) [@telefonica](https://iplug2.discourse.group/u/telefonica)\
**Post date:** [28 December 2021 09:01 UTC](https://iplug2.discourse.group/t/samfd-with-webui/528/4 "2021-12-28T09:01:55Z")

</div>

Baby steps…

If I delete [this line](https://github.com/iPlug2/iPlug2/blob/c9d2d90e77c28d14aeecaefb505ec7b15793d1d4/WDL/wdl_base64.h#L60), I no longer get the heap error. However, the only thing I can seem to send is `NULL`.

I thought `str.c_string()` and `str.length()` would have worked for pData and dataSize, respectively, but nothing got logged in the ui.

---

<div class="post-metadata">

**Author:** ![telefonica](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/telefonica/32/224_2.png) [@telefonica](https://iplug2.discourse.group/u/telefonica)\
**Post date:** [1 January 2022 03:07 UTC](https://iplug2.discourse.group/t/samfd-with-webui/528/5 "2022-01-01T03:07:08Z")

</div>

Found the bug!

> <https://github.com/iPlug2/iPlug2/pull/813>
>
> Here are some instructions for a good PR
> 
> \- \[issue\](https://github.com/iPlug2/…iPlug2/issues/811)
> \- clearly describe the problem that the PR fixes - without quotes, the js fails to execute.
> \- This PR does one thing- check

---

<div class="post-metadata">

**Author:** ![olilarkin](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/olilarkin/32/2_2.png) [@olilarkin](https://iplug2.discourse.group/u/olilarkin)\
**Post date:** [2 January 2022 22:05 UTC](https://iplug2.discourse.group/t/samfd-with-webui/528/6 "2022-01-02T22:05:46Z")

</div>

Thanks, i’ll try it soon
