Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Uncaught TypeError: Cannot call method 'push' of undefined" error when wrapping scripts inside a function #497

Closed
sampsasaarela opened this issue Nov 21, 2012 · 9 comments

Comments

@sampsasaarela
Copy link

I think that line 107 in file https://github.com/LearnBoost/socket.io-client/blob/master/dist/socket.io.js#L107 should change like this:

})('object' === typeof module ? module.exports : (this.io = {}), this);
->
})('object' === typeof module ? module.exports : io, this);

I use reguirejs (and almond.js) to pack all my clientside code to one file and I configured requirejs packer with wrap:true. So all my code is wrapped like (function(){/* mycode */}()). After making this change my wrapped codes will work, but without it, I get the following error

Uncaught TypeError: Cannot call method 'push' of undefined 

the line which cause error is

io.transports.push('websocket');
@lboynton
Copy link

I get this too, would be interested to know the correct way to load socket.io with require.js.

@maritz
Copy link

maritz commented Feb 2, 2013

Yep, me too.

@lboynton
Copy link

lboynton commented Feb 3, 2013

@maritz Having brought this up on the requirejs mailing list, it seems the only way to do it currently is either use wrap:false or change the line of code above yourself.

@maritz
Copy link

maritz commented Feb 3, 2013

Yeah, for the time being I just set my rjs config to

paths:
  "socket.io": 'empty:'

And then I load the socket.io/socket.io as a normal request. I don't like modifying third party libs in my depoyment, so this works best for me.

@grkov90
Copy link

grkov90 commented Jul 11, 2013

I found the solution! )

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:3001/socket.io/socket.io.js", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {    
    eval.call(window, xhr.response);    
    var socket = io.connect('http://localhost:3001');
    socket.on('poll', function (data) {
          console.log(data);
          socket.emit('my other event', { my: 'data' });
        });

  }
}
xhr.send();

That is

  eval.call(window, xhr.responseText)

or

with(window){
   eval(xhr.responseText)
}

@paulbarroso
Copy link

same issue here.... as @sampsasaarela said, changing it to:

})('object' === typeof module ? module.exports : io, this);

worked for me. anyone working on integrating it?

@shihongzhi
Copy link

thx, I use this method solve this problem

@tristanMatthias
Copy link

Thanks so much, this has helped me out greatly.

@Krinkle
Copy link

Krinkle commented Jun 6, 2014

So to summarise, the problem is that it assumes it is running in the global context (when executed in a browser). Making any delivery through a closure or timed execution break in unexpected ways when it turns out that the first variable declaration was just a local variable.

I think this has been refactored in the master branch for 1.0.x, but for those still connecting to backends using the 0.9.x protocol (e.g. python gevent socketio) and using socket.io.js 0.9.16, here's another way to fix it.

Change

/*! Socket.IO.js build:0.9.16, .. */
var io = ('undefined' === typeof module ? {} : module.exports);

to

/*! Socket.IO.js build:0.9.16, .. */
io = ('undefined' === typeof module ? {} : module.exports);

or

/*! Socket.IO.js build:0.9.16, .. */
window.io = ('undefined' === typeof module ? {} : module.exports);

@rauchg rauchg closed this as completed Nov 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants