# Sending a message upon startup in a custom node

**URL:** <https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059>\
**Category:** Developing Nodes\
**Created:** [1 May 2023 08:25 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059 "2023-05-01T08:25:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [1 May 2023 08:25 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/1 "2023-05-01T08:25:07Z")

</div>

I need my node to automatically send a message upon its initialization.  
In a function node, I can just create & send a message in the function node's **On Start** tab:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e8b56a338b3e4e6d943dc2b22b3c9e2da6112a92.png)

However, when I try to apply the same practice in a custom node, the message is not sent.

```auto
module.exports = function(RED)
{
    function xxxNode(config)
	{
		RED.nodes.createNode(this,config);
		const node = this;
		
		node.send({payload:config.initVal}); // ****This doesn't work!!****

		node.on('input', function(msg, send, done)
		{
			// ...Do something...
			send(msg); // This works
			done();
		});
	}
    RED.nodes.registerType("XXX",xxxNode);
}

```

I know I can use another node to send a startup message into my custom node, but want to keep this logic hidden & self-contained.

Am I doing something wrong?

BTW, other node methods such as **node.warn()**, **node.error()** etc. also do not work outside of "on input".

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 May 2023 09:02 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/2 "2023-05-01T09:02:22Z")

</div>

I've never made a node. Only subflows.

Although others may get it, I won't get what I am about to ask:

Can you show me a _bigger picture_ of how this all fits in to your new node?

If it is a `function` node can you show me where this node is in your layout?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 May 2023 09:46 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/3 "2023-05-01T09:46:47Z")

</div>

Never tried to send a msg that early. Maybe try wrapping the send in a `setTimeout` and play with the delay to see if that works.

There is a utility function that will output a msg to the debug panel but I don't know if that can be made to output a generic msg.

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [1 May 2023 10:27 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/4 "2023-05-01T10:27:42Z")

</div>

As @TotallyInformation mentions, you have to use some type of delayed action. If you look through the [source code of the inject node](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/common/20-inject.js#L90), you will see how it is done.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 May 2023 10:29 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/5 "2023-05-01T10:29:01Z")

</div>

Could also HOW you are deploying the node change what you get?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 May 2023 10:57 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/6 "2023-05-01T10:57:25Z")

</div>

Does this help?

```auto
[{"id":"6411c4f3ce7cb3db","type":"function","z":"d188b95f33e5f7e4","name":"function 32","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nnode.send({\"payload\":\"This is a test message\"});\n\nreturn msg;","finalize":"","libs":[],"x":360,"y":90,"wires":[["fac2bc76b93fb932"]]}]

```

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [1 May 2023 12:24 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/7 "2023-05-01T12:24:29Z")

</div>

> [@TotallyInformation](#):
>
> There is a utility function that will output a msg to the debug panel but I don't know if that can be made to output a generic msg.

Thanks.  
Are you referring to **node.warn()**, **node.error()** etc.? They also don't work outside of the "on input" handler (not even in "on close").

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [1 May 2023 12:26 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/8 "2023-05-01T12:26:22Z")

</div>

> [@Trying\_to\_learn](#):
>
> Does this help?
> 
> ```auto
> [{"id":"6411c4f3ce7cb3db","type":"function","z":"d188b95f33e5f7e4","name":"function 32","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nnode.send({\"payload\":\"This is a test message\"});\n\nreturn msg;","finalize":"","libs":[],"x":360,"y":90,"wires":[["fac2bc76b93fb932"]]}]
> 
> ```

Thanks.  
This works, but as I mentioned in my original post, I want to implement this in a custom node, not a function node.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [1 May 2023 13:23 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/9 "2023-05-01T13:23:11Z")

</div>

> [@omrid](#):
>
> the message is not sent

Have you checked that `config.initVal` is defined in the contexts you are using? You could try

```auto
node.send({payload:"hello world"})

```

The suggestions about timing assume that the message is actually sent but not received. How are you expecting to see the message, warning, or error? Downstream nodes may not be deployed yet, but I would expect the sidebar or console to be ready for input.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 May 2023 15:27 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/10 "2023-05-01T15:27:47Z")

</div>

> [@omrid](#):
>
> Are you referring to **node.warn()**, **node.error()** etc.? They also don't work outside of the "on input" handler (not even in "on close").

```auto
const msg2 = RED.util.encodeObject(
    { id: this.id, z: this.z, _alias: this._alias,  
      path: this._flow.path, name: this.name, 
      topic: msg.topic, 
      msg: { payload: 'hello' } 
    },
    { maxLength: 1000 }
  )

RED.comms.publish('debug', msg2)

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 May 2023 20:45 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/11 "2023-05-01T20:45:33Z")

</div>

My example was only to show you how to do it from a `function` node.

As I said: I have never made a node.

But I'm guessing that somewhere in the scheme of things, actual nodes are used.  
(or not) 🤷‍♂️

But you could use the _mechanics_ used in this example to do what you need - maybe.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [1 May 2023 23:44 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/12 "2023-05-01T23:44:28Z")

</div>

> [@drmibell](#):
>
> Have you checked that `config.initVal` is defined in the contexts you are using? You could try
> 
> ```auto
> node.send({payload:"hello world"})
> 
> ```
> 
> The suggestions about timing assume that the message is actually sent but not received. How are you expecting to see the message, warning, or error? Downstream nodes may not be deployed yet, but I would expect the sidebar or console to be ready for input.

Thanks.  
Yes, I have also tried with "Hello" messages, and as you correctly mentioned, it seems that the message is being sent, but not received by the target nodes.  
I also assumed it's a timing issue (downstream nodes still not finished initializing), but then (as you correctly mentioned again) node warn & error messages should have been able to reach the debug pane.  
Maybe @knolleary could solve the mystery.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 May 2023 17:04 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/13 "2023-05-02T17:04:03Z")

</div>

If you log to the console (server side) then you should see the message - but the sidebar in the browser has to retry and to reconnect the web socket before it can receive any messages - so anything sent really early on is unlikely to make it to the sidebar.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [3 May 2023 07:58 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/14 "2023-05-03T07:58:17Z")

</div>

Thanks.  
Indeed, the message shows on the console. So if you say that when the node instance is starting it is still not connected to the messaging framework, it means that we probably don't have an "on start" section in custom nodes (like we have in function nodes).

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 May 2023 09:15 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/15 "2023-05-03T09:15:16Z")

</div>

> [@omrid](#):
>
> it means that we probably don't have an "on start" section in custom nodes (like we have in function nodes).

There are absolutely sections of code in a custom node that execute at different times, including code that runs on load and on start. All this is saying is that the "messaging" framework isn't ready when that code runs. A slight delay added - possibly along with a flag to make sure it only runs once - should do the job. I think there are also hooks you might be able to use as well, I don't currently use any of the hooks so I don't remember off the top of my head.

Personally, I've restructured my runtime code in a way that I think is more logical and clear so that I don't get myself confused about what is run when. Check out any of the uibuilder nodes for examples.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [3 May 2023 11:33 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/16 "2023-05-03T11:33:02Z")

</div>

> [@TotallyInformation](#):
>
> Personally, I've restructured my runtime code in a way that I think is more logical and clear so that I don't get myself confused about what is run when. Check out any of the uibuilder nodes for examples.

Thanks for your response.  
Are you able in your nodes to "know" that the node is ready, or just experiment with a delay?  
I have seen the same issue with dashboard widgets. I need to wait ~100 ms until the dashboard scoket connects, before they accept data.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [3 May 2023 11:59 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/17 "2023-05-03T11:59:49Z")

</div>

> [@omrid](#):
>
> Are you able in your nodes to "know" that the node is ready, or just experiment with a delay?

The runtime emits an event when the flows are started:

> <https://github.com/node-red/node-red/blob/67dd7e30faf1a6fca07f5b745de75f4ad3036ce4/packages/node_modules/%40node-red/runtime/lib/flows/index.js#L405-L407>

When you listen to this event & act accordingly, you'll be on the save side...

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [3 May 2023 13:05 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/18 "2023-05-03T13:05:07Z")

</div>

> [@ralphwetzel](#):
>
> When you listen to this event & act accordingly, you'll be on the save side...

Thanks!  
I'll try it

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 May 2023 20:03 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/19 "2023-05-03T20:03:56Z")

</div>

> [@omrid](#):
>
> Are you able in your nodes to "know" that the node is ready

To be honest, I've never had to worry about it too much. I've several async functions in the main uibuilder node's libraries but they all seem to be ready in time for them to be used.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/146b355688ac3c2579c7cd50b87e3157927f680f.png)

> [@omrid](#):
>
> I have seen the same issue with dashboard widgets. I need to wait ~100 ms until the dashboard scoket connects, before they accept data.

That will be about the fact that Dashboard has to send quite a bit of data to the browser and then it all needs initialising and running before anything else can happen. It's a bit of a monster. But hey, it works. 🙂

> [@ralphwetzel](#):
>
> The runtime emits an event when the flows are started:

Yes, I forgot about that. Though I do, in fact use it. This is in the `runtimeSetup` function in uibuilder's runtime:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8cbaaee830a909c38b3757b5cc6c7dafc8f043b8.png)

However, it happens well before the flows are started and therefore well before messages can be "sent" between nodes:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c7f7e6b15b59d94ae2f95d56f5659dc5afb47c4f.png)

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [3 May 2023 20:20 UTC](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059/20 "2023-05-03T20:20:03Z")

</div>

> [@TotallyInformation](#):
>
> However, it happens well before the flows are started and therefore well before messages can be "sent" between nodes:

Looks like your launch message is created `on('runtime-event', ...)` ... which is another event, yet not the one I was referring to.

[Next page](https://discourse.nodered.org/t/sending-a-message-upon-startup-in-a-custom-node/78059.md?page=2)
