4

I have just installed these nodejs and socket.io but I am having problems with getting the client to connect to the server.

In my server I have:

var http = require('http');
http.createServer(function (req, res) {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.end('Hello World\n');
}).listen(3000, 'localhost');
console.log('Server running at http://localhost:3000/');
var io = require('socket.io').listen(3000);
io.sockets.on('connection', function (socket) {
 socket.emit('Hi.') ;
});

And on my client:

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('connect', function () {
 socket.emit('set nickname', confirm('What is your nickname?'));
 socket.on('ready', function () {
 console.log('Connected !');
 socket.emit('msg', confirm('What is your message?'));
 });
});

I am getting a number of errors in Chrome inspector:

GET http://localhost:9261/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined
Marcelo Cantos
187k40 gold badges338 silver badges366 bronze badges
asked Apr 9, 2012 at 9:34

6 Answers 6

2

you are not serving the client via nodeJS, this will not work :

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

try using the this instead:

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
answered Apr 9, 2012 at 14:04
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried that, I don't get the 404 anymore, just get require and io not defined.
are you sure you have installed socket.io ? you must open a terminal, go to the same directory as your server and then write 'npm install socket.io'
im using windows, I used command prompt to install it in my current app folder (im using dreamweaver), it installed socket.io into a folder called node_modules, so the directory is like this: F:\xampp\htdocs\mySite\node_modules\socket.io
2

It seems like you're sending only the 'Hello World' to the client, not the html with the client code.

Your server code should look like this (from http://socket.io/#how-to-use ):

var app = require('http').createServer(handler)
 , io = require('socket.io').listen(app)
 , fs = require('fs')
app.listen(80);
function handler (req, res) {
 fs.readFile(__dirname + '/index.html',
 function (err, data) {
 if (err) {
 res.writeHead(500);
 return res.end('Error loading index.html');
 }
 res.writeHead(200);
 res.end(data);
 });
}
io.sockets.on('connection', function (socket) {
 socket.emit('news', { hello: 'world' });
 socket.on('my other event', function (data) {
 console.log(data);
 });
});

Your client code should be in index.html. Also make sure that you have a folder named socket.io that contains the script socket.io.js

answered Apr 9, 2012 at 12:01

7 Comments

I have gone through it a couple of times but I still can't seem to get it working. I now have the errors: Uncaught ReferenceError: require is not defined Uncaught ReferenceError: io is not defined
"require is not defined" - seems like you're using require on the client side (which is wrong). How are you running the server?
From command prompt using the command: node server.js, it runs fine with no errors.
By the way I'm running the server on the same machine as I am trying to run the client, is this not possible? Must be on a different machine/VM?
The code above is now giving me an error when I try to run the server: Cannot find module 'socket.io'
|
1

It is not able to connect because your client is not able to find socket.io.js you can resolve by using proper file, from node_modules\socket.io\node_modules\socket.io-client\dist directory use socket.io.js and put it into your local directory and use it in script tag.

answered Dec 13, 2012 at 13:42

Comments

1

<script src="http://localhost:3000/socket.io/socket.io.js"></script>var socket = io.connect("http://localhost:3000");

for this way client connected to node server.This will work

answered Jan 6, 2015 at 12:10

Comments

0

Also note that when you move to production the code in your client should be updated:

var socket = io.connect('http://localhost:3000');

To something like

var socket = io.connect('my_host');

I've sent the my_host to the client side script with the page where I'm loading socket.io, by extracting the parameters req.headers.host, so for me it looks like:

 io.connect('http://' + host)

You can expose variables on the client side through express-expose.

answered Feb 22, 2014 at 13:56

Comments

0

This is likely because io was not properly instantiated, thus Uncaught ReferenceError: io is not defined

// Instantiate without argument
var io = require('socket.io')();
// Instantiate with new keyword
var Server = require('socket.io')
 , io = new Server();
// Instantiate io server with app as argument
var app = require('http').createServer(handlerFunc)
 , io = require('socket.io')(app);
app.listen(80);
// Attaching socket.io with express server
var app = express()
 , server = require('http').createServer(app)
 , io = require('socket.io')(server);
server.listen(80, handlerFunc);
answered Dec 7, 2014 at 7:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.