49

Possible Duplicate:
socket.io.js not found

I am using the most up to date version of Socket.IO and I'm trying the first example on the how to use page, however, I'm getting an error when I try to get to '/socket.io/socket.io.js'

The javascript console saying the file doesn't exist. When I check my browser (both http://socket.io/socket.io.js and https://socket.io/socket.io.js), it isn't up there either. Is the documentation out of date? Where is the socket.io.js file that I'm supposed to include?

I am running this on a Mac with NodeJS running my app.js server.

I've tried using "http://cdn.socket.io/stable/socket.io.js" but it seems that is an old version (0.6) and the Javascript console says it doesn't have the "connect" function. (which is weird, I would think that connect is a rather integral function to have in such a library, but I guess not)

Community
  • 1
  • 1
Esaevian
  • 1,707
  • 5
  • 18
  • 30

2 Answers2

79

Your Socket.IO server will handle serving the correct version of the Socket.IO client library; you should not be using one from elsewhere on the Internet. From the top example on the Socket.IO website:

<script src="/socket.io/socket.io.js"></script>

This works because you wrap your HTTP server in Socket.IO (see the example at How To Use) and it intercepts requests for /socket.io/socket.io.js and sends the appropriate response automatically.

Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
  • 4
    So if the server is running, /socket.io/socket.io.js should be there magically? Perhaps something is up with my server then, all I have is: `node app.js info - socket.io started` So I'm not sure what else is up, the Javascript console is saying that that file doesn't exist. – Esaevian Dec 31 '11 at 18:57
  • Do you mind showing your server and client code? Either add it to your question in code blocks, or put them on http://gist.github.com/ or http://pastebin.com and link them in your question. Makes it much easier to determine what might be going wrong. :) – Michelle Tilley Dec 31 '11 at 19:01
  • 7
    I copy and pasted the code from the first example here: http://socket.io/#how-to-use However, I changed the port since `node` complained that I was already using 80. Just wirked it out with primatology that I had to change the port for the socket.io.js call, so the line would be `` – Esaevian Dec 31 '11 at 19:02
  • Indeed. Are you by chance using an older version of Socket.IO? Check in the `node_modules` folder (assuming you use `npm install socket.io`) and look in Socket.IO's `package.json`, near the very top, to double check. Also, you can leave off the host/port and just put `/socket.io/socket.io.js` to let the browser determine the domain/port. – Michelle Tilley Dec 31 '11 at 19:06
  • I used the `npm install socket.io` command today, so I assume I got v.8. When I run the server, I get a `Welcome to socket.io.` on localhost:8080/socket.io/node_modules...is that right? Same on localhost:8080/socket.io/package.json – Esaevian Dec 31 '11 at 20:55
  • @MichelleTilley I'm using `/socket.io/socket.io.js` in my chat.ejs file. My project is like below, but it doesn't find the file! :( [image](https://i.stack.imgur.com/yodmL.png) – Mostafa Ghadimi Sep 07 '18 at 12:31
  • And yet, it does not. – Peter Kionga-Kamau Mar 06 '22 at 00:47
0

I know this seems obvious, but did you install Socket.IO on your server? Then you'll get a local copy of the file at '/socket.io/socket.io.js'.

npm install socket.io
benesch
  • 5,239
  • 1
  • 22
  • 36
  • I did. There is no folder called socket.io at / on my machine. I was able to run "node app.js" just fine, and the server is running. – Esaevian Dec 31 '11 at 18:48
  • 1
    It'll be at: http://127.0.0.1:1337/socket.io/socket.io.js Replace 1337 with the port you're listening on. – benesch Dec 31 '11 at 18:55
  • All I get is "Oops! Google Chrome could not connect to 127.0.0.1:1337" – Esaevian Dec 31 '11 at 18:58
  • 5
    Oh. Derp. I'm using port 8080 (node complained that I was already using 80). So the js on the client should be – Esaevian Dec 31 '11 at 19:01
  • Exactly. should point to the same location, not sure why it didn't work for you. :P Enjoy! – benesch Dec 31 '11 at 19:03
  • 3
    -1: There is no "local copy" at "/socket.io/socket.io.js". That can sound like there is a file on your server at that location. – Alexander Bird Aug 28 '12 at 05:36
  • You need to serve that .js module to the web browser – ssoto Feb 25 '14 at 10:38
  • I am following the feathers tutorial and this was exactly what I needed to get this working! Thanks – Dom Mar 16 '17 at 17:48