MagicMirror² v2.4.1 is available! For more information about this release, check out this topic.
Please read the release notes carefully since this update requires adjustments to your Raspberry Pi configuration!

Unable to sendSocketNotification from node_helper to Module



  • Hi community, I’m pulling my hair out trying to get this one working.

    I’ve got my MMM-IFTTT module nearly completed, except for a small hickup.

    In the node_helper.js I’m sending a socket notification that is not getting through to the UI. It was working at some stage before nesting it in another function, so I assume it’s a scope issue. I’ve tried debugging everything everywhere, even using this.io to send it the hard way.

    I’m using the new ES6 anonymous function syntax, ie:

    this.expressApp.post('/IFTTT', (req, res) => {
    

    which should keep the this reference from outside of the function and all my console logging suggests it does, however I think that the io reference is not the same one somehow, and despite the this.io.of(this.name).emit(notification, payload); succeeding in the node helper library, the notification simply doesn’t show in the UI dev console.

    This is also apparent when trying the var self = this; method, using the self object even immediately doesn’t send through the socket.

    Any help on this scope stuff would be appreciated! Thanks in advance.

    Raspbian Jessie
    Node version: v6.6.0
    


  • @jc21 did you try the common way?

    this.sendSocketNotification('MSG', 'Hello World');
    

    and in the module itself

    socketNotificationReceived: function(notification, payload){
        if(notification === 'MSG'){
            Log.info(payload);
        }
    }
    


  • I dumbed the module right down:

    node_helper.js

    const NodeHelper = require('node_helper');
    
    module.exports = NodeHelper.create({
        start: function() {
            console.log('[IFTTT] Starting node_helper');
    
            var self = this;
            setTimeout(function () {
                self.sendSocketNotification('MSG', {message: 'test'});
            }, 10000);
        }
    });
    

    MMM-IFTTT.js

    //...
        socketNotificationReceived: function(notification, payload) {
            if (notification === 'MSG'){
                Log.info(payload);
            }
        },
    //...
    

    Still not getting the log in dev console 😞



  • @jc21 If I’m right you must start the connection from the module, but can’t test it right now

    MMM-IFTTT.js

    start: function() {
            this.sendSocketNotification('START', {message: 'start connection'});
    },
    
    socketNotificationReceived: function(notification, payload) {
            if (notification === 'MSG'){
                Log.info(payload);
            }
        },
    

    node_helper.js

    const NodeHelper = require('node_helper');
    
    module.exports = NodeHelper.create({
        start: function() {
            console.log('[IFTTT] Starting node_helper');
        },
    
        socketNotificationReceived: function(notification, payload) {
            if (notification === 'START'){
                setTimeout(() => {
                    this.sendSocketNotification('MSG', {message: 'test'});
                 }, 10000);
            }
        },
    });
    


  • Wow that is not obvious or documented.

    However, that worked 🙂 Thanks heaps!