Fetching data with websocket

  const fetchSubscriptions = () => {
        let options = [];

        // setTimeout(500);


        if (e.selectedIndex >= 0) {
          let selHost = e.options[e.selectedIndex].value; //                                              Gets the value of only the selected server
          let address = selHost;
          let port = $("#node-input-port").val();
          const ip = address + port;
          console.log("Subscriptions triggered");

          const url =

            .then((res) => res.json())
            .then((data) => {
              data.forEach((item) => {
                  v: `UNSUBSCRIBE ${item} WebEvent/1.0`,
                  t: item.substring(30, 60),
              for (let i = 0; i < options.length; i++) {
                let value = options[i].v;
                let text = options[i].t;
                  $("<option></option>").attr("value", value).text(text)

Any idea why when i close the node my websocket works fine but in console i get an error "Cannot Read Property Value of Undefined
Everytime the value changes on websocket it logs that error:

When the node is opened it doesnt give the error and it works fine.

What WebSocket? There is no sign of a WebSocket in the code you have shared.

It doenst matter. Just this code is important. I get the error only when my node dialog is closed. When its opened it all works. Its like it forgets the option selected

The title of this topic is 'fetching data with websocket' - but there's no explanation about what the websocket has to do with this code.

Is the websocket connection being made in the browser, or in the runtime?

Which console are you getting that error? The browser console or the runtime log?

What have you done to identify which property is undefined and where that is?

Ws connection happens when i select a certain option in dropdown.
The error i get is in the browser console.
the error is following => VM854:259 Uncaught TypeError: Cannot read property 'selectedIndex' of undefined
This error comes only if my node is closed. When its opened the selected index is 1.
The property undefined is the e.selectedIndex in the code i've sent above.

If the node dialog is closed, then why have you still got code running in the editor? Your oneditsave must clean up anything you have done, like create websocket connections.

But the fact remains, you aren't showing us the code that is logging the error. Until you do that, we cannot help.

1 Like

The code is running because its my websocket, It runs everytime the value changes. I think it forgets the value in my dropdownThe server value i get in dropdown is stored in global context. How could i make it remember the value. I made the code on oneditsave to remember the value after closing. But since the values i get from global context i dont know how to make it selected
Ill send u the code in a sec

EDIT : Here is the code:

 $.getJSON("getGlobalHostConfig", function (data) {
        let server = [];
        data.forEach((item) => {
            v: item.Host,
            t: item.Host,
        console.log("getGlobalHostConfig" + server);
        for (let i = 0; i < server.length; i++) {
          let value = server[i].v;
          let text = server[i].t;
            $("<option></option>").attr("value", value).text(text) //                                   For every element in the select tag append appropriate value and text (   <option value="#">#</option>    )

You still don't clearly explain what the websocket is doing in relation to your node's edit dialog. What are you using the websocket for?

You haven't shared the code that is hitting the error you've asked us for help with.

Why cant i unsubscribe something from the websocket. When i click on the option and then click buttonSub it subscribes the option to the websocket. Then it fetches the subscribed options and puts it in another dropdown. When i select option from that other dropdown and click on the buttonUnSub it unsubscribes it.That all works fine when i 1st open the node and subscribe. When i reopen the node i cant unsubscribe it anymore.

 const unsubscribe = () => {
        let e = $("#node-input-server")[0];

        if (e.selectedIndex >= 0) {
          let selHost = node.selectValue; //                                              Gets the value of only the selected server
          let address = selHost;
          let port = node.portValue;
          const ip = address + ":" + port;

          let urlPath = `ws://${ip}/webevent/`;
          //   console.log("UnSubscribe: " + urlPath);

          if (!wsOpened) {
            globalws = new WebSocket(urlPath);

            // console.log("globalws recreated");

            globalws.onopen = () => {
              //   console.log("wsOpened:" + wsOpened);
          } else {

          globalws.onmessage = (e) => {

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