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
6 Answers 6
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>
3 Comments
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
7 Comments
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.
Comments
<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
Comments
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.
Comments
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);