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.

    [Solved] Socket notification not working

    Scheduled Pinned Locked Moved Development
    7 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.
    • W Offline
      willfri
      last edited by willfri

      Hello,

      yesterday I’ve created my first module with some socket notifications and everything worked.
      Today I started to creating my second module but now I got a problem with the socket notifications. I tried very much but it’s still not working correctly. After this I created a very simple module based on the helloworld module.

      The first notification receive at node_helper.js but when I now try to answer with a new notification nothing happend. The module still shows “Hello World!” insteand of “Started”. Also console.log() inside of socketNotificationReceived in main file are ignored when node_helper.js sends one.

      Here you can see my code.

      helloworld.js

      Module.register("helloworld",{
      	defaults: {
      		text: "Hello World!"
      	},
      
      	getDom: function() {
      		var wrapper = document.createElement("div");
      		wrapper.innerHTML = this.config.text;
      		return wrapper;
      	},
      
      	start: function() {
      		this.sendSocketNotification('CONFIG', this.config);
      	},
      
      	socketNotificationReceived: function(notification, payload) {
      		if (notification === 'STARTED') {
      			this.config.text = 'Started';
      			this.updateDom();
      		}
      	}
      });
      

      node_helper.js

      var NodeHelper = require("node_helper");
      
      module.exports = NodeHelper.create({
      	start: function () {
      		this.config = {}
      	},
      
      	socketNotificationReceived: function (notification, payload) {
      		if (notification === 'CONFIG') {
      			this.config = payload;
      			this.sendSocketNotification('STARTED');
      		}
      	}
      });
      

      Can anyone help me?

      Thanks
      Willy

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        j.e.f.f Project Sponsor Module Developer @willfri
        last edited by j.e.f.f

        @willfri I’m not positive that this is the problem, but in your node_helper.js file, you’re sending the socket notification back to the main module without a payload, but the front end expects a payload.

        Try changing this:

        this.sendSocketNotification('STARTED');
        

        to this:

        this.sendSocketNotification('STARTED', 'something');
        

        See if that fixes it.

        W 1 Reply Last reply Reply Quote 0
        • W Offline
          willfri @j.e.f.f
          last edited by

          @j.e.f.f Thanks but still not working.

          J 1 Reply Last reply Reply Quote 0
          • J Offline
            j.e.f.f Project Sponsor Module Developer @willfri
            last edited by j.e.f.f

            @willfri Some things to check… sometimes I have problems like this. Is the module file named helloworld.js? Is it in the modules under a subdirectory named helloworld? Are there any other modules installed named helloworld? there can only be one.

            If those are fine, what you can do is add console.log() at various places in your code to try and determine where it’s failing. Any console.log you add in node_helper.js will appear in the terminal where you started MM with npm start, and any console.log entries that you make in helloworld.js will appear in electron’s console (use npm start dev) to start electron with the dev console open.

            so for example, in helloworld.js, maybe add this :

            	start: function() {
            		this.sendSocketNotification('CONFIG', this.config);
                            console.log("notification sent to node_helper");
            	},
            

            and in node_helper.js

            	socketNotificationReceived: function (notification, payload) {
            		if (notification === 'CONFIG') {
                                    console.log("CONFIG notification received");
            			this.config = payload;
            			this.sendSocketNotification('STARTED');
                                    console.log("STARTED notification sent back to front end");
            		}
            	}
            

            back in helloworld.js:

            	socketNotificationReceived: function(notification, payload) {
            		if (notification === 'STARTED') {
                                    console.log("STARTED notification received from node_helper");
            			this.config.text = 'Started';
            			this.updateDom();
            		}
            	}
            

            If you see all four of those messages, then the notifications are working and you need to look elsewhere. Otherwise, when one of the messages doesn’t show up when you expect it to, then you’ve now zeroed closer into the problem.

            If you haven’t done so already, install MM locally on your laptop or workstation. It’s way easier to develop modules that way that directly on the Pi.

            W 1 Reply Last reply Reply Quote 0
            • W Offline
              willfri @j.e.f.f
              last edited by

              @j.e.f.f Thank you for your help.
              Now it works but I can’t tell exactly why. In meantime I installed a new image with Raspbian and MM and tested my code above. Then I recognized that there is also the module “helloworld” in “defaults” folder. After renaming the module it worked. Then I flashed my old installation again and tried the above code under a new name again and it worked. Finally I tried my second module, for which I do all this testing, with minimal changes and, surprise, it works.

              I think my problem was to rely too much on the output of the console. I never saw the output from console.log() in the socketNotificationReceived() (in main file) so I suspected a mistake with the socket notifications.

              It’s strange that all console.logs() in socketNotificationReceived() are missing but now everything works.
              Is it a feature?! ;D

              J 1 Reply Last reply Reply Quote 0
              • J Offline
                j.e.f.f Project Sponsor Module Developer @willfri
                last edited by

                @willfri you need to look in several places for the console.log output. Anything from node_helper.js will be displayed in the terminal window (I.e. Where you ran npm install from) or in pm2’s log file (pm2 logs mm) if you’re running it on auto start.

                The console.log statements in helloworld.js will be output in electron’s console (enable it with npm start dev).

                W 1 Reply Last reply Reply Quote 0
                • W Offline
                  willfri @j.e.f.f
                  last edited by

                  @j.e.f.f I tried npm start dev but I don’t got the output from console.log() in helloworld.js

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    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