If you can’t tell, I’m still pretty confused how this works; I can’t even test anything. The error that keeps coming up is
ReferenceError: io is not defined. Some sites used something like
var io = require('socket.io');. But then it results in the same error:
ReferenceError: require is not defined.
<script src="/socket.io/socket.io.js"></script>, but this makes no sense to me. Many sites also imply the use of node.js, but I never see it anywhere.
How can I make this work?
I tried it in Chrome, and it’s giving me an
Uncaught ReferenceError: require is not defined for the socket.io.js file. So I decide to source in require.js right before it. Then it gives the error
Uncaught Error: Module name "socket.io-client" has not been loaded yet for context. Since I’m not using this, I care not. When I try the connection, though, it gives the same
io is not defined error. When I define it as
var io = require('socket.io'), the error is
Error: Module name "socket.io" has not been loaded yet for context: _ http://requirejs.org/docs/errors.html#notloaded. I looked at the website, and it doesn’t help me at all. When I try to put “require” as a function argument, another error occurs:
TypeError: undefined is not a function.
I found the answer for anyone who gets immensely confused by the horrible lack of documentation of
You cannot source
you must source
The server automatically does the rest for you.
I solved it myself by changing index.html to import the socket io client from bower, first i installed the bower component:
bower install socket.io-client
then i changed the reference in index.html to :
Or file could be found at – lib/socket.io-client/dist/socket.io.js
write server side code in socket.io.js file and try src=”/socket.io/socket.io.js”
hope this will solve your problem
When getting socket.io to work with many other libraries using require.js I had same error, it turned out to be caused because of trying to load the socket.io.js file from the same /js folder than the rest of the other files.
Placing it in a separated folder, fixed it for me, you can see the code in this gist but all I changed for making it work, was this:
Not sure about the reason of this behavior, but I hope it helps you.
I managed to blunder through this, and squandered about an hour, on something that turned out to be a very basic error.
When an function is not defined? Such as ” Uncaught ReferenceError: io is not defined “. Does that not mean that the function is getting “used” before it is “created”?
<script src='./js/playerChatter.js'></script> <!-- this one calls io --> <script src="http://localhost:2019/socket.io/socket.io.js"></script><!-- This Creates io -->
and i changed it to this
<script src="http://localhost:2019/socket.io/socket.io.js"></script> <!-- This Creates io --> <script src='./js/playerChatter.js'></script> <!-- this on calls io -->
So now the item “io”, whether it is an object or function… Is actually getting created before it is getting used 😀
This looks like your browser cannot find the socket.io.js file. You could try opening the index.html on your computer with Firefox+Firebug or the Chrome Web Developer Tools and look at how the .js file is requested. On the other side, you could check the logs on the webserver serving the .js file whether there are any file not found errors.
require function would be provided by e.g. RequireJS, but you would still need to configure the paths to your scripts correctly for it to work.
For me after debugging through all of the very helpful suggestions, it turned out to be simply that my node server had stopped. I had been running it manually in a terminal window during dev.
Make sure your node [yourservercode].js is running on the specified port! :-]
I use jspm.