Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io connection event not firing in firefox

Im having something like the below code.

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost:8080');
  socket.on('connect', function(){
      socket.on('some-event', function(data) {});
  });
  socket.on('disconnect', function(){});
</script>

Inside the connect callback I have some code that responds to messages. This works perfectly fine on chrome. On first page load it works fine on firefox. If you reload the page then the connect event does not get called.

Im using 1.4.8 version of server and js client

like image 916
Prasanth Avatar asked Sep 11 '16 07:09

Prasanth


1 Answers

I solved it using the following code. Not very clean but for the time being this helped us to progress with the project. As you can see the problem is the connect event not firing after a page reload, so I decided to attach the events after a timeout if connect was never fired.

function attachEventListners() {
    socket.on('some-event', function(data) {});
}

var attached = false;
socket.on('connect', function(){
      attachEventListners();
      attached = true;
});

setTimeout(function() {
    if (!attached) {
        attachEventListners();
    }
}, 1000);
like image 58
Prasanth Avatar answered Oct 07 '22 22:10

Prasanth