Thanks @Sean. Of course the problem relates to the this
reference, which changes depending on the scope where it’s referenced. Thanks for pointing that out!
Read the statement by Michael Teeuw here.
Best posts made by micsa
-
RE: sendSocketNotification() fails
Latest posts made by micsa
-
How to dynamically show/hide and configure other modules from my module?
How can I dynamically (via API calls)
- Show/hide other modules (pre-configured in config.js)
- Change their configuration and apply configuration dynamically?
Case example:
I would like to dynamically show the standard weather forecast module, and configure the module to show the forecast of a specific location which name or ID I would pass dynamically.
-
RE: sendSocketNotification() fails
Thanks @Sean. Of course the problem relates to the
this
reference, which changes depending on the scope where it’s referenced. Thanks for pointing that out! -
sendSocketNotification() fails
I am trying to build a new module where the node_helper acts as an WebSocket server which it uses to listen events from external programs. Once data would be received from WS, it would send it to core module via sendSocketNotification(). However, this function always results an error when I try to call it (even with some dummy data).
my node helper:
var NodeHelper = require("node_helper"); const SERVER_PORT = 9000; const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: SERVER_PORT }); module.exports = NodeHelper.create({ // Subclass start method. start: function() { console.log("*******Starting module: " + this.name); console.log("Starting websocket server on port: " + SERVER_PORT); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { //console.log('received: %s', message); console.log("Unparsed message: " + message); parsedMessage = JSON.parse(message); console.log("Parsed message: " + parsedMessage); //this.sendSocketNotification(message.messageType, message.payload); this.sendSocketNotification("FOOBAR", {key: "value"}); }); ws.send('something'); }); console.log("*******Started module: " + this.name); } });
server console log:
Ready to go! Please point your browser to: http://localhost:8080 Create new calendar fetcher for url: http://www.calendarlabs.com/templates/ical/US-Holidays.ics - Interval: 300000 Create new news fetcher for url: http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml - Interval: 300000 Unparsed message: {"messageType":"speech_transcript","payload":{"alternatives":[],"isFinal":false,"stability":0.009999999776482582,"transcript":"the","confidence":0}} Parsed message: [object Object] Whoops! There was an uncaught exception... TypeError: this.sendSocketNotification is not a function at WebSocket.incoming (/Users/makelm/node-projects/MagicMirror/modules/mami/node_helper.js:22:14) at emitTwo (events.js:106:13) at WebSocket.emit (events.js:191:7) at Receiver._receiver.onmessage (/Users/makelm/node-projects/MagicMirror/node_modules/ws/lib/WebSocket.js:146:54) at Receiver.dataMessage (/Users/makelm/node-projects/MagicMirror/node_modules/ws/lib/Receiver.js:389:14) at Receiver.getData (/Users/makelm/node-projects/MagicMirror/node_modules/ws/lib/Receiver.js:330:12) at Receiver.startLoop (/Users/makelm/node-projects/MagicMirror/node_modules/ws/lib/Receiver.js:165:16) at Receiver.add (/Users/makelm/node-projects/MagicMirror/node_modules/ws/lib/Receiver.js:139:10) at Socket._ultron.on (/Users/makelm/node-projects/MagicMirror/node_modules/ws/lib/WebSocket.js:142:22) at emitOne (events.js:96:13)