1

I installed socket.io in the app directory (along with express) and in the index.html file I link to it this way:

<script src="http://localhost:8080/node_modules/socket.io/lib/socket.io.js"></script>

I get a 404 on this request. I double-checked the link and it's fine. the node.js app runs on port 8080 and I have no idea why it doesn't work

UPDATE: Apparently it is something to with Express. When not using it I don't have that problem and socket.io works fine. This is he setup of my server file:

var express = require('express'),
    http = require('http');

var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

app.listen(8000);

and this is the front-end:

<script src="/socket.io/socket.io.js"></script>
<script src="js/vendor/jquery-1.8.3.min.js"></script>
<script>
  var socket = io.connect('http://localhost:8000');

Also, I switched the jQuery from google CDN to local and it doesn't find it too

ilyo
  • 35,851
  • 46
  • 106
  • 159

4 Answers4

7

Make sure you've made socket.io listening to external connections in your back-end. Basically socket.io has inside method of providing your socket.io.js file, so you should place your require ('socket.io').listen(app) before defining any routes to your app.

The correct url is :

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

as randunel said.

So, your configuration should look like this :

UPDATED for express 3.x :

var express = require('express'),
    http = require('http');

var app = express();
var server = http.createServer(app).listen(8000);

require('socket.io').listen(server);  // Your app passed to socket.io

Server-side

<script src="http://localhost:8000/socket.io/socket.io.js"></script>
drinchev
  • 19,201
  • 4
  • 67
  • 93
  • I updated my question to show the server file, is that what you mean? – ilyo Jan 09 '13 at 10:47
  • I get a bunch of errors: `listen` method expects `http.Server` and then `io is not defined` – ilyo Jan 09 '13 at 11:52
  • 1
    this is because of express 3.x i've updated the answer. This exact code work for me ;) .. try ```curl http://localhost:8000/socket.io/socket.io.js``` and you should see ``` debug - served static content /socket.io.js``` in your console – drinchev Jan 09 '13 at 12:03
  • could you share your code? The warning is gone, but `io` is still `undefined`... I'm going crazy here.. thanx for the help :) – ilyo Jan 09 '13 at 12:15
  • 1
    Well there is no io defined since I don't need that, you can define io, by changing ```var io = require('socket.io').listen(server);``` – drinchev Jan 09 '13 at 12:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22453/discussion-between-drinchev-and-ilyad) – drinchev Jan 09 '13 at 12:17
3

Don't use that whole path, it does not correspond to your project's directory structure. Just use

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

By the way, make sure that socket.io is also listening on 8080, not just the other services in your project.

randunel
  • 8,917
  • 1
  • 26
  • 24
0

Try putting your JS file in http://localhost:8080/javascripts/socket.io.js, basically the JavaScript directory of your public folder.

Amberlamps
  • 39,180
  • 5
  • 43
  • 53
  • which js file? the server file or the socket.io? – ilyo Jan 08 '13 at 16:58
  • 1
    it says nothing there about the location of the socket.io.js file. I switched to port 80 so all will be as it appears in the documentation and I still get `GET http://localhost/socket.io/socket.io.js 404 (Not Found)` – ilyo Jan 08 '13 at 21:19
0

The way is the one from randunel.

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

If you link the file directly, you can get a "Welcome to socket.io" message instead of the file. There's also a CDN socket.io.js file available to link, but it's outdated and will not work fine on new projects, so it's not recommended.

Riwels
  • 786
  • 10
  • 21