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)
    

  • Module Developer

    @micsa

    this.sendSocketNotification("FOOBAR", {key: "value"});
    

    Is here a problem?

    here, this doesn’t indicate your module, but function incoming(){...}
    So you can use these;

    var self =this; 
    wss.on('connection', function connection(ws) {
          ws.on('message', function incoming(message) {
            self.sendSocketNotification("FOOBAR", {key: "value"}
          );
    });
    

    or.

    wss.on('connection', (ws)=>{
          ws.on('message', (message)=> {
                   this.sendSocketNotification("FOOBAR", {key: "value"}
          );
    });
    

    I didn’t test it in real device, but you can understand what I mean.


  • Module Developer

    And If you use this callback in other scopes, you should bind proper object instance.



  • 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!


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.