# Sending messages inside callback functions

**URL:** <https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683>\
**Category:** Developing Nodes\
**Created:** [2 December 2019 08:48 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683 "2019-12-02T08:48:08Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [4 December 2019 03:49 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/22 "2019-12-04T03:49:52Z")

</div>

Github is [https://github.com/Bobo-amg/Node-Red-Daily-Scheduler](https://github.com/Bobo-amg/Node-Red-Daily-Scheduler)

Appreciate any assistance.

Cheers

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [4 December 2019 05:26 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/23 "2019-12-04T05:26:54Z")

</div>

I can have a look this evening. Can you commit a small test flow as well?

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [4 December 2019 05:46 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/24 "2019-12-04T05:46:36Z")

</div>

Nothing needed except Inject on one side and Debug on the other. Then edit the local time and add a description. Deploy and Inject.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [4 December 2019 05:48 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/25 "2019-12-04T05:48:48Z")

</div>

Okay, that's simple then. I thought you needed some kind of schedule configuration for the node. 😁

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [4 December 2019 05:55 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/26 "2019-12-04T05:55:33Z")

</div>

Well I did design it to accept input from something like the ui form node, yes. So the real use case (for me) is using that (via msg.alarm\_date). But for testing (and learning) I incorporated the same into the editor. It's possible there is a problem there somewhere.

It is also possible that I didn't clone the msg correctly in my attempts, although I did try several different code methods.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [4 December 2019 06:11 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/27 "2019-12-04T06:11:10Z")

</div>

By _editor_ you mean that you enter the values as node properties in the UI?

Because you set `editor` or `inputmsg` depending on whether the message had the `alarm_date` properties or not.

I can look at it this evening (CET) in detail... I think I will need to restructure and clean that code up a bit. 🙈😀

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [4 December 2019 06:20 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/28 "2019-12-04T06:20:40Z")

</div>

Just out of curiosity... I haven't used any of the existing scheduler nodes yet, but what is this node supposed to do differently? 😀

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [4 December 2019 06:23 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/29 "2019-12-04T06:23:43Z")

</div>

> [@kuema](#):
>
> By _editor_ you mean that you enter the values as node properties in the UI?

Yes, in the node's editor.

> [@kuema](#):
>
> I think I will need to restructure and clean that code up a bit

Go for it, although isn't javascript meant to be "all over the place"? Coming from the old days, this stuff hurts me 😜

Also bear in mind that I have stripped some code out and added some "redundant" code in, in an attempt to find the issue. 🤓

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [4 December 2019 06:28 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/30 "2019-12-04T06:28:50Z")

</div>

> [@kuema](#):
>
> what is this node supposed to do differently?

Well ... 🤔

I started building this a long time ago (then lost my allocated play time to continue it), and since then a couple of other scheduling nodes have come to market. I continued with this one because it was as much a learning experience as it was solving an issue for me.

It is however only meant to be a day based scheduler (for testing right now it is MINUTE based though- you do not have to wait days for events to fire 🤣), nothing else. To that end I added a few things like monthly recurring alarms, early reminder alarms etc.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [7 December 2019 05:28 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/31 "2019-12-07T05:28:00Z")

</div>

Just wondering if you had a chance to take a look yet?

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [7 December 2019 09:11 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/32 "2019-12-07T09:11:37Z")

</div>

I have already forked it, set up a dev environment and started a simple flow. But some other thing came up, I think I can continue next week. 🙈

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [7 December 2019 10:39 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/33 "2019-12-07T10:39:51Z")

</div>

Np. Thanks.

---

<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:** [7 December 2019 14:05 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/34 "2019-12-07T14:05:06Z")

</div>

Lost track of the thread at the end there. In case it is of help, here is a basic test node that I use to play with and that shows how I'm now handling the `send()` function so that it works correctly with pre and post v1.

```auto

'use strict'

/** Dummy logging 
 * @type {Object.<string, function>} */
var dummyLog = {
    fatal: function(){}, // fatal - only those errors which make the application unusable should be recorded
    error: function(){}, // error - record errors which are deemed fatal for a particular request + fatal errors
    warn: function(){}, // warn - record problems which are non fatal + errors + fatal errors
    info: function(){}, // info - record information about the general running of the application + warn + error + fatal errors
    debug: function(){}, // debug - record information which is more verbose than info + info + warn + error + fatal errors
    trace: function(){}, // trace - record very detailed logging + debug + info + warn + error + fatal errors
}
var log = dummyLog // reset to RED.log or anything else you fancy at any point

const moduleName = 'nrtest'

/** Export the function that defines the node */
module.exports = function(RED) {
    // NB: entries in settings.js are read-only and shouldn't be read using RED.settings.get, that is only for settings that can change in-flight.
    // see Node-RED issue #1543.

    /* RED.events.on('nodes-started',function() {
            console.log(' ******All nodes have started******')
        })
        RED.events.on('nodes-stopped',function() {
            console.log(' ******All nodes have stopped******')
        }) 
    */

    //#region ----- Constants (& logging) for standard setup ----- //

    /** Folder containing settings.js, installed nodes, etc. @constant {string} userDir */
    //userDir = RED.settings.userDir
    
    /** Root URL path for http-in/out and uibuilder nodes @constant {string} httpNodeRoot */
    //const httpNodeRoot = RED.settings.httpNodeRoot

    //#region ----- back-end debugging ----- //
    log = RED.log
    log.trace('[NRTEST:Module] ----------------- module started -----------------')
    //#endregion ----- back-end debugging ----- //

    //const app = RED.httpNode // || RED.httpAdmin

    //#endregion -------- Constants -------- //

    /** Run the node instance - called from registerType()
     * @param {Object} config The configuration object passed from the Admin interface (see the matching HTML file)
     */
    function nodeGo(config) {
        // Create the node
        RED.nodes.createNode(this, config)

        /** A copy of 'this' object in case we need it in context of callbacks of other functions. @constant {Object} node */
        const node = this
        log.trace('[NRTEST:nodeGo] = Keys: this, config =', {'this': Object.keys(node), 'config': Object.keys(config)})

        //var context = this.context().flow
        console.log('[NRTEST:nodeGo] ', {'this': node, 'config': config})

        //#region ----- Create local copies of the node configuration (as defined in the .html file) ----- //
        // NB: node.id and node.type are also available
        node.name = config.name || ''
        node.topic = config.topic || ''
        //#endregion ----- Local node config copy ----- //

        /** Handler function for node input events (when a node instance receives a msg)
         * @see https://nodered.org/blog/2019/09/20/node-done 
         * @param {Object} msg The msg object received.
         * @param {function} send Per msg send function, node-red v1+
         * @param {function} done Per msg finish function, node-red v1+
         **/
        function nodeInputHandler(msg, send, done) {

            // If this is pre-1.0, 'send' will be undefined, so fallback to node.send
            send = send || function() { node.send.apply(node,arguments) }
            // If this is pre-1.0, 'done' will be undefined, so fallback to dummy function
            done = done || function() { if (arguments.length>0) node.error.apply(node,arguments) }

        } // -- end of msg received processing -- //

        // Process inbound messages
        node.on('input', nodeInputHandler)

        // Do something when Node-RED is closing down
        // which includes when this node instance is redeployed
        node.on('close', function(removed,done) {
            log.trace(`[NRTEST:nodeGo:onClose] ${removed?'Node Removed':'Node (re)deployed'}`)

            node.removeListener('input', nodeInputHandler)

            done()
        })

    } // ---- End of nodeGo (initialised node instance) ---- //

    /** Register the node by name. This must be called before overriding any of the
     * Node functions. */
    RED.nodes.registerType(moduleName, nodeGo)

} // ==== End of module.exports ==== // 

// EOF

```

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [15 December 2019 02:14 UTC](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683/35 "2019-12-15T02:14:10Z")

</div>

I have refactored the code to make it simpler to debug. It sits in the updated branch. Also opened an issue.  
If anyone has the time to take a look I would be most grateful.

> **[Bobo-amg/Node-Red-Daily-Scheduler](https://github.com/Bobo-amg/Node-Red-Daily-Scheduler)**
>
> Contribute to Bobo-amg/Node-Red-Daily-Scheduler development by creating an account on GitHub.

[Previous page](https://discourse.nodered.org/t/sending-messages-inside-callback-functions/18683.md?page=1)
