Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Websocket not sending patch

    Scheduled Pinned Locked Moved
    Third-party software
    2
    3
    269
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • CarlosRundefined
      CarlosR
      last edited by

      Hi,

      I'm trying to subscribe via websocket to the object model using a node.js client.

      let objectModel = null;
        wsClientDuet.on("connectFailed", function (error) {
          console.log("Connect Error");
        });
        wsClientDuet.on("connect", (conn) => {
          connectionDuet = conn;
          console.log("Connected to Duet WebSocket Server");
          connectionDuet.on("error", function (error) {
            console.log("Connection Error");
          });
          connectionDuet.on("close", function () {
            console.log("Connection to Duet is closed");
          });
          connectionDuet.on("message", function (message) {
            console.log(message.utf8Data);
            if (message.utf8Data === "PONG\n") {
              return;
            }
            if (objectModel === null) {
              objectModel = message.utf8Data;
              console.log("Object model received");
              return;
            }
            let ack = "OK\n";
            connectionDuet.sendUTF(ack);
          });
        });
        setInterval(async () => {
          if (connectionDuet.connected) {
            let message = "PING\n";
            connectionDuet.sendUTF(message);
          }
        }, 5000);
      
        await wsClientDuet.connect(`url`, "");
      
      

      The server is replying with PONG after the PINGS.
      Also, the first object model is sent. But it seems the OK is not working since the server is not sending any patch.

      Is there anything wrong in the code?

      Thank you.

      chrishammundefined 1 Reply Last reply Reply Quote 0
      • chrishammundefined
        chrishamm administrators @CarlosR
        last edited by

        @CarlosR We also provide the @duet3d/connectors NPM package but I don't know if it works in standalone NodeJS applications. See here for the message handling in SBC mode.

        Duet software engineer

        CarlosRundefined 1 Reply Last reply Reply Quote 0
        • CarlosRundefined
          CarlosR @chrishamm
          last edited by

          @chrishamm Thank you so much!

          It didn't work indeed in standalone nodejs. However, that was easy to fix (in case anyone is interested).

          1. Install @duet3d/connectors, @duet3d/objectmoddel and xhr2 package:
          npm install @duet3d/connectors 
          npm install @duet3d/objectmodel
          npm install xhr2
          
          1. Import XMLHttpRequest from xhr2 package in BaseConnector.js file
          const XMLHttpRequest = require("xhr2");
          
          1. Import WebSocket from ws package in RestConnector.js:
          const WebSocket = require('ws');
          

          Then everything work as normal.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Unless otherwise noted, all forum content is licensed under CC-BY-SA