I added a listener for the deployment event nodes:deploy, but it did not trigger

I added a listener for the deployment event nodes:deploy, but it did not trigger. Other events, such as flows:started, can be triggered. There are also the following input events that can be triggered. hope to get help.
I mainly want to save the current flowId when deploying for my business

module.exports = function (RED) {
  console.log("init: Can trigger");

  RED.events.on("nodes:deploy", function () {
    console.log("nodes:deploy: Here Can't trigger");
  });
  RED.events.on("flows:started", function () {
    console.log("flows:started: Can trigger");
  });

  function Device(config) {
    RED.nodes.createNode(this, config);
    this.on("input", function (msg) {
      console.log("Received:", msg.mydata);
      this.send(msg);
    });
  }
  RED.nodes.registerType("device", Device);
};

I only found the RED.events.emit('deploy') event - perhaps that's what you're looking for.

This method doesn't seem to work eitherļ¼ŒI tried multiple ways of writing, but only one event triggered

module.exports = function (RED) {
  RED.events.on("flows:started", function () {
    // Only this event can trigger
    console.log("Trigger flows:started");
  });
  // Neither of these fire when I click the deploy button in the upper right corner
  RED.events.emit("deploy", function () {
    console.log("Trigger deploy");
  });
  // Neither of these fire when I click the deploy button in the upper right corner
  RED.events.emit("nodes:deploy", function () {
    console.log("Trigger nodes:deploy");
  });

  function Device(config) {
    RED.nodes.createNode(this, config);
    this.on("input", function (msg) {
      console.log("Received:", msg.mydata);
      this.send(msg);
    });
  }
  RED.nodes.registerType("device", Device);
};

Your using emit instead of on - emit generates the event, on defines the trigger.

The deploy event is an Editor event - not a Runtime event. It can only be used in the editor-side code.

flows:started is the main runtime event, albeit undocumented, that is available for this type of thing.

Could you please tell me about the editor-side code you are talking about, where do you add content? Are you writing in the dragged node? I hope this event is a global event because I need to obtain the final flow information during deployment to associate it with the business. Or do you have any better suggestions?

Well, I tried this, but it had no obvious effect. Is there any example that I can refer to?

I think I should try using flows:started to listen to flow creation and update events.

It would probably help if you described why you wanted to react to these events. Typically you do not need to. There may be other ways to look at the issue.

Thanks.I jump to NodeRed from the front-end page with parameters. I need to set up the process for each device and associate the device ID with flowId. But I couldn't get the flowId, so I thought of listening to the deployment event. I also thought about getting it in the process and then sending it to the backend through http request, but this would be very troublesome, so I hope it can be done through a global event.

The flow ID should be available once you have initialised a node instance. In your case, that will be in the Device function AFTER calling createNode. At that point, the this object should contain the data I think, if not, you can query it since this certainly contains the node id.

If you want to know the flow id for a given node, then look at its z property:

function Device(config) {
    RED.nodes.createNode(this, config);
    const flowId = config.z
   
  }

Yes, it can be done this way. Another question is when to associate the flow with my business equipment. I think the process will be completely completed and ready to use when I click deploy. This is the main problem. So I hoped to complete it by listening to the deployment event. In fact, I failed.

Yes, there will be an ID after initialization, but I hope to do other operations after the flow editing is completed. What is the best time to process it? At present, I only think about monitoring deployment.

I think this partly depends on whether you require Editor users to always do a full deploy. If you do, then the setup code of your node will always be triggered at that point.

On the other hand, if someone may change something that does not include your node and does a partial deploy, it won't trigger a setup in your node and in that case, you might indeed need to use an event listener.

1 Like

In this case, I can only judge whether the user has completed editing by whether it is deployed, but this can only be monitored through deployment. Or save in real time during editing, in which case you also need to monitor the change event, but the side effect is that imperfect content will replace the original process. So it seems that the best way at present is to deal with it during deployment.

I think I have to concur.

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