io is not defined -- HTML/Javascript-Meteor app - javascript

I am trying to develop a chat component having my server side code in Meteor. To establish a socket connection I want to expose io object which we get from npm library socket.io-client.
I am adding a script tag to my HTML page which looks as shown below
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
Then I am trying to connect to server side socket as shown below
var socket = io('http://localhost:3000');
This is where I get "io is not defined" error
Any leads would be appreciated.

Try using this
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
instead of
<script src="http://localhost:3000/socket.io/socket.io.js"></script>

am adding a script tag to my HTML page which looks as shown below
Use the wrapper package instead:
https://atmospherejs.com/joncursi/socket-io-client
io would be defined but its CORS issue
Follow the instructions in https://enable-cors.org/server_meteor.html

Related

i am trying to access socket.io library on client side but unable to do it , my client-side js file is main.js inside the js folder (js/main.js)

this is i have inculcated in my `chat.html` file to access the socket.io on client side ...main is my javascript file on client side
please replace file in last line as main.js file present inside js folder
<script src="/socket.io/socket.io.js"></script>
<script src="file"></script>
on console it is showing unreferenced socket.io referring to this below line
i am using this on my client side javascript file
const socket = io();

How to fix "Uncaught ReferenceError: Cannot access 'io' before initialization" in socket.io client?

Hi i am trying to create a basic realtime application in socket.io , i have completely setup the server side in node.js and the file "socket.io.js" is also being imported in browser but somehow when i try to connect with socket.io server using let io = io() or let io = io.connect() i get the following error.
Uncaught ReferenceError: Cannot access 'io' before initialization
node.js code:
client code:
After reading a comment on this post i fixed the issue by changing the let with var keyword.

Sockets Connecting

I have been trying to get my WAMP Webserver run with socket.io but I just cannot seem to get it to work.
My socket server is on port 3000 and the WAMP on port 80. I know that both servers work.
Web Server Client Code:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://[ip address]:3000');
socket.on('buzz', function (data) {
console.log(data);
});
</script>
Console Error:
Uncaught ReferenceError: io is not defined
at (index):97
UPDATE
I was missing the socket.io.js file. But now I am getting
GET http://[[IP]]:3000/socket.io/?EIO=3&transport=polling&t=LxfQwpE
net::ERR_CONNECTION_TIMED_OUT
In the console every few seconds.
As I recall, this problem was solved by installing a NGINX reverse proxy -- I believe I followed the instructions here: Socket.io with nginx
This was one of my first projects, but I thought I would point people in the right direction in case other students wish to use a WAMP backend and Socket.io.

'io is not defined' on client. included socket.io script tag in HTML

I am receiving these errors on the client:
GET /socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined
In HTML
<script src="/socket.io/socket.io.js"></script>
In client JS:
var socket = io.connect();
I have also tried adding the local server as some other posts suggested. (localhost:3000). I have used sockets before in this exact way without an error, so I am confused.
Try access
http://localhost:3000/socket.io/socket.io.js
directly via your web browser to see it there's a javascript after you start socket.io server. If even your web browser cannot access it, client side has nothing to do with this problem. You must check your server side if it start correctly and port 3000 hasn't been used before by another process including your previous socket.io server that may not be killed from last session. Try
ps aux | grep node
in your shell to see if there's already node process or not before start new socket.io server.

socket.io - ReferenceError: io is not defined

I am writing an application for Android 2.3.5 (that will also be compatible with iOS). I wish to transfer data from the app's HTML/Javascript to a Python program on a server (which uses the Twisted engine to retrieve the data).
I've tried many things and looked at various forums, answers, tutorials, and web pages--including most of them here--and can't find an answer. Here's the relevant Javascript I have in my index.html file:
<script src="socket-lib/socket.io.js"></script>
<script type="text/javascript" charset="utf-8">
function sendData() {
try {
var socket = io.connect('http://mywebsite.com:12345');
socket.on('connect', function(data) {
socket.send('Hello.');
socket.on('message', function (msg) {
socket.send('This is where I send data?');
});
});
}
catch(err) {
alert('ERROR: socket.io encountered a problem:\n\n' + err);
}
} // end of sendData
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.
I put the socket-lib folder in assets/www, where any other Javascript source should go. This is also where the index.html file is. Many sites use <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?
Reply edits:
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 socket.io.
You cannot source /socket-lib/socket.io.js,
you must source http://yourwebsite.com:12345/socket.io/socket.io.js.
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 :
<script src="bower_components/socket.io-client/socket.io.js"></script>
Or file could be found at - lib/socket.io-client/dist/socket.io.js
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"?
In the part of my HTML file, that "calls" the javaScript files, it looked like this :
<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 :D
Have FUN!
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:
instead of:
socketio: 'socket.io',
Use:
socketio: '../socket.io/socket.io',
Not sure about the reason of this behavior, but I hope it helps you.
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.
The 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.
Add this:
import 'btford/angular-socket-io/mock/socket-io';
In my case, using Unity WebGL with C# UnitySocketIO plugin, I need to add delay (1 sec) between Init and Connect, because the first method did not have time to add a new JS Socket object and code (listeners) to the page to work with it..
As mentioned in other answers, you have to use the complete URL.
<script src="http://127.0.0.1:5565/socket.io/socket.io.js "></script>
if it's giving you an error 404 please check whether io is properly instantiated on your server.js.If not please refer following to instantiate the io properly.
<script>
var socket = io();
</script>
In your server.js
const app = express();
const http = require('http'); //from express module
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
got to url /socket.io/socket.io.js and check if a js file is served or not if yes then instead of writing
var socket = io();
write
window.onload= function(){
var socket = io();
}

Categories