I've followed the exact same steps which have always previously worked for me, create application through express, place the module dependencies in the node_modules folder. It appears that the socket.io client-side javascript file isn't being found.
(I've looked at other peoples fixes, which is to include the JavaScript file in a script tab. I have not had to do this for my previous node + socket.io projects).
JavaScript on client:
var socket = io.connect('http://localhost');
JavaScript on server:
var io = require('socket.io').listen(app);
node_modules folder:
socket.io, which has an internal node_modules folder containing socket.io-client
Error Message:
Uncaught ReferenceError: io is not defined
(anonymous function)
When I include the socket.io client manually: http://cdn.socket.io/stable/socket.io.js
I get a different error which is:
Uncaught TypeError: Object #<Object> has no method 'connect'
(anonymous function)
Node.js newbie here! I am pretty sure this has been answered. Yet I kept finding problems with the src for the socket. Apparently this :
<script src="/socket.io/socket.io.js">
was not working for me on the client side.I've replaced the above line with this and it seems to work fine.
(Edit: although this is obvious, this link may not work depending on when you are reading this answer. Please pick the latest link from: https://cdnjs.com/libraries/socket.io)
Here is a working client side code:
On the server side (handles only 1 socket)
On the client, did you do:
before you set the
socket
variable?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 look like this :
and i changed it to this
So now the item "io", whether it is an object or function... Is actually getting created before it is getting used :D
Have FUN!
On the client: