i keep on getting the error /socket.io/socket.io.js 404 (Not Found) Uncaught ReferenceError: io is not defined
my code is
var express = require('express'), http = require('http'); var app = express(); var server = http.createServer(app); var io = require('socket.io').listen(server); server.listen(3000);
and
<script src="/socket.io/socket.io.js"></script>
what is the problem ???
any help is welcome!
In this guide we'll create a basic chat application. It requires almost no basic prior knowledge of Node. JS or Socket.IO, so it's ideal for users of all knowledge levels.
Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of: a Node. js server: Source | API. a Javascript client library for the browser (which can be also run from Node.
Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you.
Copying socket.io.js
to a public folder (something as resources/js/socket.io.js
) is not the proper way to do it.
If Socket.io
server listens properly to your HTTP
server, it will automatically serve the client file to via http://localhost:<port>/socket.io/socket.io.js
, you don't need to find it or copy in a publicly accessible folder as resources/js/socket.io.js
& serve it manually.
Code sample
Express 3.x - Express 3 requires that you instantiate a http.Server
to attach socket.io
to first
var express = require('express') , http = require('http'); //make sure you keep this order var app = express(); var server = http.createServer(app); var io = require('socket.io').listen(server); //... server.listen(8000);
Happy Coding :)
How to find socket.io.js for client side
install socket.io
npm install socket.io
find socket.io client
find ./ | grep client | grep socket.io.js
result:
./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js
copy socket.io.js to your resources:
cp ./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js /home/proyects/example/resources/js/
in your html:
<script type="text/javascript" src="resources/js/socket.io.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With