Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js /socket.io/socket.io.js not found

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!

like image 698
hausinho Avatar asked Oct 17 '13 12:10

hausinho


People also ask

Does Socket.IO require node js?

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.

What is Socket.IO Socket.IO js?

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.

Does Socket.IO need a server?

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.


2 Answers

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 :)

like image 163
Amol M Kulkarni Avatar answered Sep 20 '22 07:09

Amol M Kulkarni


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> 
like image 26
ZiTAL Avatar answered Sep 20 '22 07:09

ZiTAL