socket.broadcast.emit doesn't fire correctly










0














Node create a unique url and bind socket.io to it.



var io = require("socket.io").listen(server, path: req.originalUrl);


When a client connects also binds his socket.io-client to that url



var socket = io('192.168.1.101:3000', path: window.location.pathname);


I don't have problems and everything works great.



When a client performs a particular action, server do



socket.broadcast.emit("foo"); //I made console.log here and it prints


client-side:



socket.on("foo", () => console.log("okay"));


The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.



I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
error










share|improve this question


























    0














    Node create a unique url and bind socket.io to it.



    var io = require("socket.io").listen(server, path: req.originalUrl);


    When a client connects also binds his socket.io-client to that url



    var socket = io('192.168.1.101:3000', path: window.location.pathname);


    I don't have problems and everything works great.



    When a client performs a particular action, server do



    socket.broadcast.emit("foo"); //I made console.log here and it prints


    client-side:



    socket.on("foo", () => console.log("okay"));


    The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.



    I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
    error










    share|improve this question
























      0












      0








      0







      Node create a unique url and bind socket.io to it.



      var io = require("socket.io").listen(server, path: req.originalUrl);


      When a client connects also binds his socket.io-client to that url



      var socket = io('192.168.1.101:3000', path: window.location.pathname);


      I don't have problems and everything works great.



      When a client performs a particular action, server do



      socket.broadcast.emit("foo"); //I made console.log here and it prints


      client-side:



      socket.on("foo", () => console.log("okay"));


      The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.



      I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
      error










      share|improve this question













      Node create a unique url and bind socket.io to it.



      var io = require("socket.io").listen(server, path: req.originalUrl);


      When a client connects also binds his socket.io-client to that url



      var socket = io('192.168.1.101:3000', path: window.location.pathname);


      I don't have problems and everything works great.



      When a client performs a particular action, server do



      socket.broadcast.emit("foo"); //I made console.log here and it prints


      client-side:



      socket.on("foo", () => console.log("okay"));


      The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.



      I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
      error







      node.js websocket socket.io






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 17:07









      Giulio

      84




      84






















          1 Answer
          1






          active

          oldest

          votes


















          0














          socket.broadcast.emit("foo");


          broadcasts a message to all connected clients EXCEPT the one specified by socket. If you want to broadcast to all connected clients, then use:



          io.broadcast.emit("foo");



          You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.






          share|improve this answer




















          • yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
            – Giulio
            Nov 11 at 17:49











          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%2f53251130%2fsocket-broadcast-emit-doesnt-fire-correctly%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














          socket.broadcast.emit("foo");


          broadcasts a message to all connected clients EXCEPT the one specified by socket. If you want to broadcast to all connected clients, then use:



          io.broadcast.emit("foo");



          You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.






          share|improve this answer




















          • yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
            – Giulio
            Nov 11 at 17:49
















          0














          socket.broadcast.emit("foo");


          broadcasts a message to all connected clients EXCEPT the one specified by socket. If you want to broadcast to all connected clients, then use:



          io.broadcast.emit("foo");



          You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.






          share|improve this answer




















          • yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
            – Giulio
            Nov 11 at 17:49














          0












          0








          0






          socket.broadcast.emit("foo");


          broadcasts a message to all connected clients EXCEPT the one specified by socket. If you want to broadcast to all connected clients, then use:



          io.broadcast.emit("foo");



          You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.






          share|improve this answer












          socket.broadcast.emit("foo");


          broadcasts a message to all connected clients EXCEPT the one specified by socket. If you want to broadcast to all connected clients, then use:



          io.broadcast.emit("foo");



          You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 17:27









          jfriend00

          427k53548593




          427k53548593











          • yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
            – Giulio
            Nov 11 at 17:49

















          • yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
            – Giulio
            Nov 11 at 17:49
















          yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
          – Giulio
          Nov 11 at 17:49





          yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
          – Giulio
          Nov 11 at 17:49


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53251130%2fsocket-broadcast-emit-doesnt-fire-correctly%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo