7

As I understood it, from http://socket.io/#how-to-use, node.js automatically serves the socket.io file on the server.

I have installed socket.io with npm install socket.io and I can see that it resides in node_modules one level above the server root.

server.js:

    var static = require('./plugins/node-static');
var socketIO = require('socket.io');
var clientFiles = new static.Server('./client');

var http = require('http');
httpServer = http.createServer(function (request, response) {
    request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(8253);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) { .....

index.html:

<html>
<head>
    <title>Chat</title>
</head>
<body>
    <script src="/socket.io/socket.io.js"></script>
    <script type="text/javascript"
            src="http://code.jquery.com/jquery-1.5.2.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            var webSocket = new io.Socket('localhost', { port: 8253 });
            webSocket.connect(); .......

Starting the server works fine, but when opening index.html, I receive the following error:

GET http://localhost:8253/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined                 :8253/:25

Ideas?

Soroush Hakami
  • 5,226
  • 15
  • 66
  • 99
  • "I can see that it resides in node_modules one level above the server root." What do you mean by "the server root"? Not confused with any traces of apache, perhaps? Modules installed by NPM reside in the NPM repos, they don't have to be anywhere in relation to your project's main node script. A successful `require('socket.io')` indicates that the installation is OK and the problem is in the code. – Kos Dec 24 '11 at 23:09

4 Answers4

6

Try listening on the server after you bind it with socket.io

Place this

httpServer.listen(8253);

after

var webSocket = socketIO.listen(httpServer);
fent
  • 17,861
  • 15
  • 87
  • 91
3

Edited: Apologies, I have written something that did not answer your question.

On the client side you need the following:

var socket = io.connect(); //Hostname and port not required - Autodetected
socket.on('connect', function(){
  $('#status').text('Connected');
});
socket.on('message', function(m){
 $('#message').text(m);
});
socket.on('disconnect', function(){
 $('#status').text('Disconnected');
});

Working example => https://github.com/parj/node-websocket-demo/blob/master/public/main.js

NPM Information (if required): If you are in Linux

cd <location of your server.js>
npm install -g socket.ion #install globally
npm link socket.io. #Create a symbolic link 

If you are on Windows you can't do npm link

cd <location of your server.js>
npm install socket.io

Your directory structure should look like

server.js
node_modules/ #Directory - same level as server.js
    socket.io #socket.io underneath that

node_modules should be in the same directory as server.js, not above server root

First Zero
  • 21,586
  • 6
  • 46
  • 45
  • I can't remember myself ever needing to perform such magics, the intro tutorial also doesn't suggest that. `npm install socket.io` should do the trick – Kos Dec 24 '11 at 23:04
  • BTW "if you are on Windows" does npm support windows now? – Kos Dec 24 '11 at 23:04
  • 1/npm on Windows => cf stackoverflow article - http://stackoverflow.com/questions/7300132/how-to-use-npm-with-node-exe 2/ Magic? :) The tutorials talk about npm install. What they miss out is, what if you have several projects with the same library. Then for each project do you go and run npm install? The better solution (imo) is install it globally `npm install -g` and then create a link to it `npm link`. That way you only have to install at one place and upgrade only one place. – First Zero Dec 25 '11 at 06:46
  • Glad could help - if you want to do a socket emit (for transferring JSON) and custom events - another example - https://github.com/parj/node-websocket-demo/tree/socket_emit – First Zero Dec 25 '11 at 09:04
  • I believe that one of us is confusing something here :-) You're implying that `npm install` is somehow project-local, while `npm install -g` is global. I had the impression that `npm install` is local like for the current user, not a project, so (contraty to what you're saying) you don't do it one time for any project. And most of all you need a root access to do a `install -g`, don't you? (I can't check here) – Kos Dec 25 '11 at 15:41
  • Also [npm docs](http://npmjs.org/doc/folders.html) recommends a local install for packages which you're going to `require()`. – Kos Dec 25 '11 at 15:42
  • Also thanks for the update on `node.js` status on Windows, there wasn't a "standalone" Windows version of node/npm and it didn't work very well on Cygwin or MSys so I decided to set up a virtualbox with linux and a mapped drive (which turned out to be an awesome solution in the end). Good to know there's an alternative now! – Kos Dec 25 '11 at 15:44
  • Actually, you are right :-) .. Since I am on a Virtual Machine with Ubuntu, so of course I have root access and most at workplace will not and usually developers would like to control what version they use. Thanks for your comments. – First Zero Dec 25 '11 at 17:24
2

When you are converting from a regular express app:

const express = require('express')
const app = express()
app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

It is important to do two things:

One(that one I believe everyone gets right):

var server = require('http').Server(app);
var io = require('socket.io')(server);

Two(this one is very easy to miss) : Call server.listen instead of app.listen

I spent almost two hours debugging this, that's why I'm documenting.

kubal5003
  • 7,186
  • 8
  • 52
  • 90
0

You need to add localhost to your script src tag like below

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

or alternatively you can same file which locates in node_modules folder

<script src="http://localhost:8253/node_modules/socket.io/client-dist/socket.io.js"></script>
X999
  • 450
  • 3
  • 15