4

I'm trying to run a node.js application on my freebsd server, but I can't get the socket.io library to work with it. I've tried including:

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

Which gives a 404 error, and if i link directly to the file (i.e. where it is in my public_html folder) I get the io not defined error.

Thanks in advance

Amadan
  • 191,408
  • 23
  • 240
  • 301
Jack TC
  • 344
  • 1
  • 5
  • 17

4 Answers4

12

Try creating another node.js application that has this single line in it and then run it with node.js

var io = require('socket.io').listen(8000);

Then in your browser visit http://127.0.0.1:8000 and you should get the friendly "Welcome to socket.io." greeting. If you are getting this then socket.io is running and will serve the socket.io.js file.

The only other thing that I can think of that might be happening is that you might not be linking to the alternate port in your client file. Unless you're running the socket.io server on express which is running on port 80. For now create a client file that has the script source for socket.io set to

<script src="http://127.0.0.1:8000/socket.io/socket.io.js"> </script>

This should connect to the socket.io server running on port 8000 and get the socket.io.js file.

sonikarc
  • 137
  • 5
  • Thanks, this didn't happen, I'll see about fixing it. – Jack TC May 14 '12 at 10:16
  • I'm so confused. I am having the same problem, and this does work, tho I definitely want to fix it without running a separate server. "The only other thing that I can think of that might be happening is that you might not be linking to the alternate port in your client file" What do you mean by that, I'm using express on port 3200, is there anything special I have to do with socket.io for it to server the client js file on port 3200? – Bob Monteverde Jun 02 '13 at 20:03
3

Your node.js application still has to serve it - it does not get served automagically. What do you have in your server? It should be something like

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

or similar (the listen is important). The location is not a real location on the disk - socket.io library should intercept the URL and serve its clientside library, as far as I understand.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

Add the following after body parser:

, express.static(__dirname + "/public")

So something like:

var app = module.exports = express.createServer(
  express.bodyParser()
  , express.static(__dirname + "/public")
);
vimdude
  • 4,447
  • 1
  • 25
  • 23
0

For those who got the same kind of issue if they run (open) your html file directly from your local file directory(ex: file:///C:/Users/index.html).

Solution: You have to run(open) the file through localhost (ex: http://localhost:3000/index.html) where the server is listening to.

Below code snippet shows how to create a server and how to wire together with the express and socket.io

const express = require("express");
const app = express();
const httpServer = require("http").createServer(app); 
const io = require("socket.io")(httpServer);

///////////////////////////////////////////////////////////////
// Any other server-side code goes here                     //
//////////////////////////////////////////////////////////////

httpServer.listen(3000, () => {
    console.log(`Server listening to port 3000`);
});
SridharKritha
  • 8,481
  • 2
  • 52
  • 43