Read the statement by Michael Teeuw here.
[Solved] Socket notification not working
-
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 -
@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.
-
@j.e.f.f Thanks but still not working.
-
@willfri Some things to check… sometimes I have problems like this. Is the module file named
helloworld.js
? Is it in themodules
under a subdirectory namedhelloworld
? Are there any other modules installed namedhelloworld
? 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. Anyconsole.log
you add innode_helper.js
will appear in the terminal where you started MM withnpm start
, and anyconsole.log
entries that you make inhelloworld.js
will appear in electron’s console (usenpm 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.
-
@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 -
@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 rannpm 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 withnpm start dev
). -
@j.e.f.f I tried
npm start dev
but I don’t got the output fromconsole.log()
inhelloworld.js