• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Unable to sendSocketNotification from node_helper to Module

Scheduled Pinned Locked Moved Development
5 Posts 2 Posters 3.6k Views 2 Watching
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.
  • J Offline
    jc21
    last edited by Sep 19, 2016, 6:27 AM

    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
    
    S 1 Reply Last reply Sep 19, 2016, 8:21 AM Reply Quote 0
    • S Offline
      strawberry 3.141 Project Sponsor Module Developer @jc21
      last edited by Sep 19, 2016, 8:21 AM

      @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);
          }
      }
      

      Please create a github issue if you need help, so I can keep track

      1 Reply Last reply Reply Quote 0
      • J Offline
        jc21
        last edited by Sep 19, 2016, 8:35 AM

        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 :(

        S 1 Reply Last reply Sep 19, 2016, 9:30 AM Reply Quote 0
        • S Offline
          strawberry 3.141 Project Sponsor Module Developer @jc21
          last edited by Sep 19, 2016, 9:30 AM

          @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);
                  }
              },
          });
          

          Please create a github issue if you need help, so I can keep track

          1 Reply Last reply Reply Quote 1
          • J Offline
            jc21
            last edited by Sep 19, 2016, 9:44 AM

            Wow that is not obvious or documented.

            However, that worked :) Thanks heaps!

            1 Reply Last reply Reply Quote 1
            • 1 / 1
            1 / 1
            • First post
              1/5
              Last post
            Enjoying MagicMirror? Please consider a donation!
            MagicMirror created by Michael Teeuw.
            Forum managed by Sam, technical setup by Karsten.
            This forum is using NodeBB as its core | Contributors
            Contact | Privacy Policy