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.

    sendSocketNotification() fails

    Scheduled Pinned Locked Moved Solved Bug Hunt
    4 Posts 2 Posters 2.2k 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.
    • M Offline
      micsa
      last edited by

      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)
      
      ? 1 Reply Last reply Reply Quote 0
      • ? Offline
        A Former User @micsa
        last edited by A Former User

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

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

        or.
        2)

        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.

        1 Reply Last reply Reply Quote 2
        • ? Offline
          A Former User
          last edited by

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

          1 Reply Last reply Reply Quote 0
          • M Offline
            micsa
            last edited by

            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!

            1 Reply Last reply Reply Quote 1
            • 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