VueJS with vue-socket-io not displaying anything2019 Community Moderator ElectionGet 'id' value from vue-router's linkBabel causing Uncaught TypeError in transpiled ES5 Vue component codeVue Socket.io Not Receiving DataHow to fire ajax requests from multiple Vue components mount() in succession[Vue warn]: Error in nextTick: "NotFoundError: Failed to execute 'insertBefore' on 'Node'how to call (vanilla) websocket.onmessage method in vue instanceVue socket io does not work (cannot get it to fire connection events)Background Sync with Workbox in Vuevue and sockert error- the response must not be the wildcard '*' when the request's credentials mode is 'include'Vuejs create a websocket connection in only one vue component

What has been your most complicated TikZ drawing?

Calculus II Professor will not accept my correct integral evaluation that uses a different method, should I bring this up further?

Latest web browser compatible with Windows 98

Making a sword in the stone, in a medieval world without magic

How could a scammer know the apps on my phone / iTunes account?

Rejected in 4th interview round citing insufficient years of experience

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

Russian cases: A few examples, I'm really confused

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Use of プラトニック in this sentence?

Why doesn't the EU now just force the UK to choose between referendum and no-deal?

Instead of Universal Basic Income, why not Universal Basic NEEDS?

Replacing Windows 7 security updates with anti-virus?

Why doesn't using two cd commands in bash script execute the second command?

How to explain that I do not want to visit a country due to personal safety concern?

Define, (actually define) the "stability" and "energy" of a compound

How to generate globally unique ids for different tables of the same database?

It's a yearly task, alright

Is it normal that my co-workers at a fitness company criticize my food choices?

Sword in the Stone story where the sword was held in place by electromagnets

Dot in front of file

Did CPM support custom hardware using device drivers?

Could the Saturn V actually have launched astronauts around Venus?

RegionDifference for Cylinder and Cuboid



VueJS with vue-socket-io not displaying anything



2019 Community Moderator ElectionGet 'id' value from vue-router's linkBabel causing Uncaught TypeError in transpiled ES5 Vue component codeVue Socket.io Not Receiving DataHow to fire ajax requests from multiple Vue components mount() in succession[Vue warn]: Error in nextTick: "NotFoundError: Failed to execute 'insertBefore' on 'Node'how to call (vanilla) websocket.onmessage method in vue instanceVue socket io does not work (cannot get it to fire connection events)Background Sync with Workbox in Vuevue and sockert error- the response must not be the wildcard '*' when the request's credentials mode is 'include'Vuejs create a websocket connection in only one vue component










0















Trying for days but without any luck.
I installed https://www.npmjs.com/package/vue-socket.io.



I then added this code to my main.js vue file:



import VueSocketio from 'vue-socket-io';
Vue.use(VueSocketio, 'http://localhost:3001/');


My node server is running on port 3001, and when I refresh my page I do see something happen i.e. the command prompt shows a random string i.e. "PLlVISTMrfO2BzCJAAAZ". This is my connection, so that part is working!



When I want to receive a message from my server I used this code:



io.on('connection', function(socket) 

console.log('connected with id: ' + socket.id)

socket.on('SEND_MESSAGE', function(data)
io.emit('hello_world', 'Get you right back...')
console.log(data);
);

);


When I send a message from Vue, it is received by the server, but when I try to send a message from the server to Vue it is never received.



My Vue code:



 created() 

// Send a message to the server
this.$socket.emit('SEND_MESSAGE', 'Hello Node!')

// Listen
this.$options.sockets.hello_world = (data) =>
console.log(data);
console.log('something came...');





Does anyone have any idea how to fix this? I also tried this but this aint working either:



 sockets: 
connect()
// Fired when the socket connects.
console.log("server connected");
,

disconnect()
console.log("server disconnected");
,

hello_vue: function (data)
console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')











share|improve this question




























    0















    Trying for days but without any luck.
    I installed https://www.npmjs.com/package/vue-socket.io.



    I then added this code to my main.js vue file:



    import VueSocketio from 'vue-socket-io';
    Vue.use(VueSocketio, 'http://localhost:3001/');


    My node server is running on port 3001, and when I refresh my page I do see something happen i.e. the command prompt shows a random string i.e. "PLlVISTMrfO2BzCJAAAZ". This is my connection, so that part is working!



    When I want to receive a message from my server I used this code:



    io.on('connection', function(socket) 

    console.log('connected with id: ' + socket.id)

    socket.on('SEND_MESSAGE', function(data)
    io.emit('hello_world', 'Get you right back...')
    console.log(data);
    );

    );


    When I send a message from Vue, it is received by the server, but when I try to send a message from the server to Vue it is never received.



    My Vue code:



     created() 

    // Send a message to the server
    this.$socket.emit('SEND_MESSAGE', 'Hello Node!')

    // Listen
    this.$options.sockets.hello_world = (data) =>
    console.log(data);
    console.log('something came...');





    Does anyone have any idea how to fix this? I also tried this but this aint working either:



     sockets: 
    connect()
    // Fired when the socket connects.
    console.log("server connected");
    ,

    disconnect()
    console.log("server disconnected");
    ,

    hello_vue: function (data)
    console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')











    share|improve this question


























      0












      0








      0








      Trying for days but without any luck.
      I installed https://www.npmjs.com/package/vue-socket.io.



      I then added this code to my main.js vue file:



      import VueSocketio from 'vue-socket-io';
      Vue.use(VueSocketio, 'http://localhost:3001/');


      My node server is running on port 3001, and when I refresh my page I do see something happen i.e. the command prompt shows a random string i.e. "PLlVISTMrfO2BzCJAAAZ". This is my connection, so that part is working!



      When I want to receive a message from my server I used this code:



      io.on('connection', function(socket) 

      console.log('connected with id: ' + socket.id)

      socket.on('SEND_MESSAGE', function(data)
      io.emit('hello_world', 'Get you right back...')
      console.log(data);
      );

      );


      When I send a message from Vue, it is received by the server, but when I try to send a message from the server to Vue it is never received.



      My Vue code:



       created() 

      // Send a message to the server
      this.$socket.emit('SEND_MESSAGE', 'Hello Node!')

      // Listen
      this.$options.sockets.hello_world = (data) =>
      console.log(data);
      console.log('something came...');





      Does anyone have any idea how to fix this? I also tried this but this aint working either:



       sockets: 
      connect()
      // Fired when the socket connects.
      console.log("server connected");
      ,

      disconnect()
      console.log("server disconnected");
      ,

      hello_vue: function (data)
      console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')











      share|improve this question
















      Trying for days but without any luck.
      I installed https://www.npmjs.com/package/vue-socket.io.



      I then added this code to my main.js vue file:



      import VueSocketio from 'vue-socket-io';
      Vue.use(VueSocketio, 'http://localhost:3001/');


      My node server is running on port 3001, and when I refresh my page I do see something happen i.e. the command prompt shows a random string i.e. "PLlVISTMrfO2BzCJAAAZ". This is my connection, so that part is working!



      When I want to receive a message from my server I used this code:



      io.on('connection', function(socket) 

      console.log('connected with id: ' + socket.id)

      socket.on('SEND_MESSAGE', function(data)
      io.emit('hello_world', 'Get you right back...')
      console.log(data);
      );

      );


      When I send a message from Vue, it is received by the server, but when I try to send a message from the server to Vue it is never received.



      My Vue code:



       created() 

      // Send a message to the server
      this.$socket.emit('SEND_MESSAGE', 'Hello Node!')

      // Listen
      this.$options.sockets.hello_world = (data) =>
      console.log(data);
      console.log('something came...');





      Does anyone have any idea how to fix this? I also tried this but this aint working either:



       sockets: 
      connect()
      // Fired when the socket connects.
      console.log("server connected");
      ,

      disconnect()
      console.log("server disconnected");
      ,

      hello_vue: function (data)
      console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')








      vue.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 at 20:09







      Ometecuthli

















      asked Mar 6 at 18:59









      OmetecuthliOmetecuthli

      105




      105






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The npm source you linked says that you have to subscribe to events to receive them dynamically, during runtime.



          this.sockets.subscribe('EVENT_NAME', (data) => 
          this.msg = data.message;
          );

          this.sockets.unsubscribe('EVENT_NAME');


          You can find it here.



          Your code is written according to the old documentation (I guess that's deprecated, no longer working).



          I suggest you try it with the subscribe method.



          EDIT



          The solution was to update the used socket package to a better one, and handle the CORS errors in the node server.



          New, better package: npmjs.com/package/vue-socket.io-extended



          CORS error handling: https://enable-cors.org/server_expressjs.html






          share|improve this answer

























          • Thanks for your reply Muka! I tried that but I get an error: Error in created hook: "TypeError: this.sockets is undefined" When I use a subscribe.

            – Ometecuthli
            Mar 6 at 20:36











          • Do you have a sockets property (object) in your component?

            – muka.gergely
            Mar 6 at 20:39












          • Oh sorry I misunderstood you, yeah I have a properties object but it doesnt do anything when I add connect() and other stuff on it. sockets: connect: function () console.log('socket connected') , customEmit: function (data) console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')

            – Ometecuthli
            Mar 6 at 20:50












          • I tried searching for "listener" and "subscribe" but I cannot find anything that works so far. Do you have any suggestions on how to handle it properly? I keep getting the same error "this.sockets is undefined", no matter how I do it.

            – Ometecuthli
            Mar 7 at 7:23











          • If you could provide code that should work (including the server), and displays the error you experience, I’ll see if I can get it to work. I tried to replicate the situation, but unfortunately I didn’t have the spare time. Maybe you can upload it to github.

            – muka.gergely
            Mar 7 at 7:57










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030370%2fvuejs-with-vue-socket-io-not-displaying-anything%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The npm source you linked says that you have to subscribe to events to receive them dynamically, during runtime.



          this.sockets.subscribe('EVENT_NAME', (data) => 
          this.msg = data.message;
          );

          this.sockets.unsubscribe('EVENT_NAME');


          You can find it here.



          Your code is written according to the old documentation (I guess that's deprecated, no longer working).



          I suggest you try it with the subscribe method.



          EDIT



          The solution was to update the used socket package to a better one, and handle the CORS errors in the node server.



          New, better package: npmjs.com/package/vue-socket.io-extended



          CORS error handling: https://enable-cors.org/server_expressjs.html






          share|improve this answer

























          • Thanks for your reply Muka! I tried that but I get an error: Error in created hook: "TypeError: this.sockets is undefined" When I use a subscribe.

            – Ometecuthli
            Mar 6 at 20:36











          • Do you have a sockets property (object) in your component?

            – muka.gergely
            Mar 6 at 20:39












          • Oh sorry I misunderstood you, yeah I have a properties object but it doesnt do anything when I add connect() and other stuff on it. sockets: connect: function () console.log('socket connected') , customEmit: function (data) console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')

            – Ometecuthli
            Mar 6 at 20:50












          • I tried searching for "listener" and "subscribe" but I cannot find anything that works so far. Do you have any suggestions on how to handle it properly? I keep getting the same error "this.sockets is undefined", no matter how I do it.

            – Ometecuthli
            Mar 7 at 7:23











          • If you could provide code that should work (including the server), and displays the error you experience, I’ll see if I can get it to work. I tried to replicate the situation, but unfortunately I didn’t have the spare time. Maybe you can upload it to github.

            – muka.gergely
            Mar 7 at 7:57















          0














          The npm source you linked says that you have to subscribe to events to receive them dynamically, during runtime.



          this.sockets.subscribe('EVENT_NAME', (data) => 
          this.msg = data.message;
          );

          this.sockets.unsubscribe('EVENT_NAME');


          You can find it here.



          Your code is written according to the old documentation (I guess that's deprecated, no longer working).



          I suggest you try it with the subscribe method.



          EDIT



          The solution was to update the used socket package to a better one, and handle the CORS errors in the node server.



          New, better package: npmjs.com/package/vue-socket.io-extended



          CORS error handling: https://enable-cors.org/server_expressjs.html






          share|improve this answer

























          • Thanks for your reply Muka! I tried that but I get an error: Error in created hook: "TypeError: this.sockets is undefined" When I use a subscribe.

            – Ometecuthli
            Mar 6 at 20:36











          • Do you have a sockets property (object) in your component?

            – muka.gergely
            Mar 6 at 20:39












          • Oh sorry I misunderstood you, yeah I have a properties object but it doesnt do anything when I add connect() and other stuff on it. sockets: connect: function () console.log('socket connected') , customEmit: function (data) console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')

            – Ometecuthli
            Mar 6 at 20:50












          • I tried searching for "listener" and "subscribe" but I cannot find anything that works so far. Do you have any suggestions on how to handle it properly? I keep getting the same error "this.sockets is undefined", no matter how I do it.

            – Ometecuthli
            Mar 7 at 7:23











          • If you could provide code that should work (including the server), and displays the error you experience, I’ll see if I can get it to work. I tried to replicate the situation, but unfortunately I didn’t have the spare time. Maybe you can upload it to github.

            – muka.gergely
            Mar 7 at 7:57













          0












          0








          0







          The npm source you linked says that you have to subscribe to events to receive them dynamically, during runtime.



          this.sockets.subscribe('EVENT_NAME', (data) => 
          this.msg = data.message;
          );

          this.sockets.unsubscribe('EVENT_NAME');


          You can find it here.



          Your code is written according to the old documentation (I guess that's deprecated, no longer working).



          I suggest you try it with the subscribe method.



          EDIT



          The solution was to update the used socket package to a better one, and handle the CORS errors in the node server.



          New, better package: npmjs.com/package/vue-socket.io-extended



          CORS error handling: https://enable-cors.org/server_expressjs.html






          share|improve this answer















          The npm source you linked says that you have to subscribe to events to receive them dynamically, during runtime.



          this.sockets.subscribe('EVENT_NAME', (data) => 
          this.msg = data.message;
          );

          this.sockets.unsubscribe('EVENT_NAME');


          You can find it here.



          Your code is written according to the old documentation (I guess that's deprecated, no longer working).



          I suggest you try it with the subscribe method.



          EDIT



          The solution was to update the used socket package to a better one, and handle the CORS errors in the node server.



          New, better package: npmjs.com/package/vue-socket.io-extended



          CORS error handling: https://enable-cors.org/server_expressjs.html







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 19:39

























          answered Mar 6 at 20:26









          muka.gergelymuka.gergely

          698615




          698615












          • Thanks for your reply Muka! I tried that but I get an error: Error in created hook: "TypeError: this.sockets is undefined" When I use a subscribe.

            – Ometecuthli
            Mar 6 at 20:36











          • Do you have a sockets property (object) in your component?

            – muka.gergely
            Mar 6 at 20:39












          • Oh sorry I misunderstood you, yeah I have a properties object but it doesnt do anything when I add connect() and other stuff on it. sockets: connect: function () console.log('socket connected') , customEmit: function (data) console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')

            – Ometecuthli
            Mar 6 at 20:50












          • I tried searching for "listener" and "subscribe" but I cannot find anything that works so far. Do you have any suggestions on how to handle it properly? I keep getting the same error "this.sockets is undefined", no matter how I do it.

            – Ometecuthli
            Mar 7 at 7:23











          • If you could provide code that should work (including the server), and displays the error you experience, I’ll see if I can get it to work. I tried to replicate the situation, but unfortunately I didn’t have the spare time. Maybe you can upload it to github.

            – muka.gergely
            Mar 7 at 7:57

















          • Thanks for your reply Muka! I tried that but I get an error: Error in created hook: "TypeError: this.sockets is undefined" When I use a subscribe.

            – Ometecuthli
            Mar 6 at 20:36











          • Do you have a sockets property (object) in your component?

            – muka.gergely
            Mar 6 at 20:39












          • Oh sorry I misunderstood you, yeah I have a properties object but it doesnt do anything when I add connect() and other stuff on it. sockets: connect: function () console.log('socket connected') , customEmit: function (data) console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')

            – Ometecuthli
            Mar 6 at 20:50












          • I tried searching for "listener" and "subscribe" but I cannot find anything that works so far. Do you have any suggestions on how to handle it properly? I keep getting the same error "this.sockets is undefined", no matter how I do it.

            – Ometecuthli
            Mar 7 at 7:23











          • If you could provide code that should work (including the server), and displays the error you experience, I’ll see if I can get it to work. I tried to replicate the situation, but unfortunately I didn’t have the spare time. Maybe you can upload it to github.

            – muka.gergely
            Mar 7 at 7:57
















          Thanks for your reply Muka! I tried that but I get an error: Error in created hook: "TypeError: this.sockets is undefined" When I use a subscribe.

          – Ometecuthli
          Mar 6 at 20:36





          Thanks for your reply Muka! I tried that but I get an error: Error in created hook: "TypeError: this.sockets is undefined" When I use a subscribe.

          – Ometecuthli
          Mar 6 at 20:36













          Do you have a sockets property (object) in your component?

          – muka.gergely
          Mar 6 at 20:39






          Do you have a sockets property (object) in your component?

          – muka.gergely
          Mar 6 at 20:39














          Oh sorry I misunderstood you, yeah I have a properties object but it doesnt do anything when I add connect() and other stuff on it. sockets: connect: function () console.log('socket connected') , customEmit: function (data) console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')

          – Ometecuthli
          Mar 6 at 20:50






          Oh sorry I misunderstood you, yeah I have a properties object but it doesnt do anything when I add connect() and other stuff on it. sockets: connect: function () console.log('socket connected') , customEmit: function (data) console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')

          – Ometecuthli
          Mar 6 at 20:50














          I tried searching for "listener" and "subscribe" but I cannot find anything that works so far. Do you have any suggestions on how to handle it properly? I keep getting the same error "this.sockets is undefined", no matter how I do it.

          – Ometecuthli
          Mar 7 at 7:23





          I tried searching for "listener" and "subscribe" but I cannot find anything that works so far. Do you have any suggestions on how to handle it properly? I keep getting the same error "this.sockets is undefined", no matter how I do it.

          – Ometecuthli
          Mar 7 at 7:23













          If you could provide code that should work (including the server), and displays the error you experience, I’ll see if I can get it to work. I tried to replicate the situation, but unfortunately I didn’t have the spare time. Maybe you can upload it to github.

          – muka.gergely
          Mar 7 at 7:57





          If you could provide code that should work (including the server), and displays the error you experience, I’ll see if I can get it to work. I tried to replicate the situation, but unfortunately I didn’t have the spare time. Maybe you can upload it to github.

          – muka.gergely
          Mar 7 at 7:57



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030370%2fvuejs-with-vue-socket-io-not-displaying-anything%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко