Skip to content

[QUESTION]: websocket usage for data updates #1590

@kheinrich188

Description

@kheinrich188

Hi there,

I’m experiencing an issue when trying to use the new WebSocket option in FUXA to update data more frequently than the 1-second interval provided by the WebAPI.

Here’s what I’ve done:

  1. Switched my WebAPI connection to an “internal” connection to retain the tags.
  2. Created a server-side script to fetch the initial data using the WebAPI.
  3. Set up the WebSocket connection to handle subsequent updates.

However, the behavior isn’t as expected. When fetching tag data by ID using $getTag(), it always returns null.

Example API Response:

From my WebAPI:

{
    "t_8f60fd-f8073b": "1",
    "t_cb52db-724556": "3",
    "t_a2d094-5596cc": "1"
}

The WebSocket returns the following:

42/fuxa,
[
    "fuxa-data",
    {
        "tagId": "t_9bece6-11c1c5",
        "data": "-21.2713623046875"
    }
]

When I try to then setTag(tagId, data) I always get setValue not supported!

Despite successfully retaining tag IDs and verifying their values using the template function $getTag(), attempting to access tags via $getTag('t_8f60fd-f8073b') consistently returns null.

console.log($getTag('t_8f60fd-f8073b' /* stHMIInterface - iCurrentUnit */));

Environment:
FUXA Version: 1.2.3

Expected Behavior:

  • Only have an internal device with tags.
  • $setTag to set the data to a internal device tag to get rid of the webapi connection

Please let me know if I’m missing something in the setup or if this is a bug. Any guidance would be much appreciated!

Thank you!

Script that I configured as 1s startup delay on server:

const readDataFromWebsocket = () => {
  // Handle successful connection
  ws.on('open', () => {
    console.log('WebSocket connection established');
    ws.send('40/fuxa');
  });

  ws.on('message', (message) => {
     try {
        const messageStr = message.toString();
        if (messageStr.startsWith('42/fuxa,')) {
          const parsedData = JSON.parse(messageStr.slice(8)); // Strip off the socket prefix "42/fuxa"
          const tagId = parsedData[1].tagId;
          const value = parsedData[1].data;
          console.log('tagId', tagId);
          console.log('value', value);
          $setTag(tagId, value);
        } else {
          console.log('Raw message:', messageStr);
        }
      } catch (error) {
        console.log('Error processing message from WebSocket server:', error);
      }
  });

  ws.on('error', (error) => {
    console.log('WebSocket error:', error);
  });

  ws.on('close', () => {
    console.log('WebSocket connection closed');
    //TODO: Attempt to reconnect after a delay if the connection is closed
  });
}

const getInitialFetchData = () => {
	const options = {
    hostname: hostname,
    port: port,
    path: getPath,
    method: 'GET',
  };

  const req = http.request(options, (res) => {
    let responseData = '';

    res.on('data', (chunk) => {
      responseData += chunk;
    });

    res.on('end', () => {
      const data = JSON.parse(responseData);
      if (data) {
        Object.entries(data).forEach(([tagId, value]) => {
           $setTag(tagId, value);
        });
        
        readDataFromWebsocket()
      } else {
        console.log('empty data');
      }
    });
  });

  req.on('error', (error) => {
    console.log('Error');
    console.log(JSON.stringify(error));
  });

  req.end();

}

getInitialFetchData()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions