Navigation

    MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    SOLVED sendSocketNotification() fails

    Bug Hunt
    2
    4
    1636
    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
      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)
      
      Sean 1 Reply Last reply Reply Quote 0
      • Sean
        Sean Module Developer @micsa last edited by Sean

        @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
        • Sean
          Sean Module Developer @micsa last edited by Sean

          @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
          • Sean
            Sean Module Developer 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
              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 Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
              This forum is using NodeBB as its core | Contributors
              Contact | Privacy Policy