Using the following simple application of socket.io, I get very strange behaviour, but only on some computers I tested. On most computers, it seems to work fine. This is the setup I used:
server.js:
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) {
i = 0;
setInterval(function() {
socket.emit('news', 'test');
}, 200);
socket.on('my other event', function (data) {
console.log(data);
});
});
index.html:
<html>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on('news', function (data) {;
console.log("test");
socket.emit('my other event', { my: 'data' });
});
</script>
</html>
<body></body>
If I set the interval on 30 instead of 200 milliseconds, it seems to work well (i.e. the client and server both receive the socket messages on time), but when I set the interval on 100 or more milliseconds, the client does not respond to the first 13 (consequently 13) messages sent by the server and then suddenly receives all 13 messages at once, directly followed by this error:
"WebSocket connection to 'ws://example.com/socket.io/1/websocket/63zOWQ5mEuzV01WuqZ5X' failed: Unrecognized frame opcode: 11".
This behaviour repeats for some time, so sets of 13 messages are suddenly received followed by the error, but sometimes, after a random amount of error sets, it starts behaving normally again. I know this sounds really weird, but I can't find the source of this problem and it greatly affects the user experience. What could trigger this problem?
I am using node.js version 0.8.24, socket.io 0.9.14 and chrome version 31.0.1650.63 m. I am running on CentOS version 6.5. This behaviour also happens on IE 10 with a different error: "SCRIPT12152: WebSocket Error: Network Error 12152"
Related
I am trying to create a simple chat application using Node js. I am using a Windows operating system. As local server I am using Xampp. I have installed Node. I have also installed socket.io using package.json. The code in package.json is given below.
{
"name":"chat",
"version":"0.0.1",
"private":"true",
"dependencies":{
"socket.io":"0.9.16",
"express":"3.4.0"
}
}
Then I have written the code for the server. The Node server is running in port 1337. The code for the server is given below.
var io = require('socket.io').listen(1337);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Then when I run it, it is running. Then I have written the code for the client in a index.php file. The code for the client is given below.
<!DOCTYPE html>
<html>
<head>
<title>Chat app.</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/node:1337/socket.io/socket.io.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var socket = io.connect('http: // localhost / node : 1337');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
});
</script>
</body>
</html>
But when I try to the run it with a browser, all I get see in the console is that access is forbidden. All my files including node_modules is saved in C:\xampp\htdocs\node.
The code you're using is copied from the socket.io Home page and it's only used as an example, but it's not actually working code because the socket.io script isn't being bound to any server instance.
Socket.io isn't a server. It's just a library for nicely handling Websockets. In order to use socket.io you have to require HTTP or Express and create a server instance. Then you'll have to bind the server instance with socket.io.
For a working implementation on how to get socket.io up and running with your server, you'll have to look at the How To Use page. There they have these nice code example, depending on the implementation you're running (if it's HTTP, or something else).
So scratch the whole Xampp server idea. Node has it's own built in server capabilities and that's what you're meant to be using.
Here's a working example (from the socket.io website) of how Socket.io is meant to be used with HTTP. In this code snippet, the server is also created (and it's listening on port 80), so you won't have to worry about that:
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);
});
});
Once your server's up and running, you can access it by typing localhost:80 into the browser.
I have simple socket io code
-- Server
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
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);
});
});
-- Client
<script src="http://192.168.1.100:8080/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.100:8080');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
there is no problem if the client script run on the server machine , but in other machine rather than server there is no problem in getting socket io javascript file , the client send request to server and I can see that the request received by server , but the client do not receive anything and it try to switch transport after 20 sec , none of them work. But as soon as I close server , the client receive the message that sent from server before ...
I use ubunto 12.04 LTS and Google chrome 28 , please help me through this problem
So i was trying to set a new project with nodejs and socket.io
at first i try these codes in my home pc at this site: http://socket.io/#how-to-use
i tried the first example and it works well so i continue my work at my home pc.
but, when i try it at my work pc my work is not working.
even the first example at the web doesn't work.
the client doesn't receive the emitted 'news' event from the server
and the client doesn't emit 'my other event' to the server.
there's no error at the client browser and i already try at all browsers i use (firefox, ie, and chrome all of them are the latest version)
why does the client doesn't emit the event to server, and the client can't receive emitted data from server?
what is the problem? is it the computer at my work? because i already tried it with 3 different computer, and the result is still the same. it only work at my home pc.
and at my work computer (win7 x86) i already disable my UAC and firewall.
i use nodejs v0.10.1, socket.io v0.9.13
here's the code that i copied from the example from socket.io:
client.html
<script src="./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8999');
setTimeout(function(){
socket.emit('my other event', { my: 'emitted by timeout' }); // nothing happened at server
},3000);
socket.on('news', function (data) {
console.log(data); // this won't fire
socket.emit('my other event', { my: 'data' }); // this won't fire
});
</script>
server.js
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8999);
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);
});
});
output from node console:
info - socket.io started
debug - client authorized
info - handshake authorized c3Y6q0EnyWtldKBKnUHp
debug - setting request GET /socket.io/1/websocket/c3Y6q0EnyWtldKBKnUHp
debug - set heartbeat interval for client c3Y6q0EnyWtldKBKnUHp
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
I'm trying to do cross-domain socket.io, but I'm experiencing an issue:
I always get XMLHttpRequest cannot load http://handsonwithnodejs.samarthwiz.c9.io/socket.io/1/?t=1358882710333. Origin https://c9.io is not allowed by Access-Control-Allow-Origin.
Server code:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(process.env.PORT);
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.set('origins', '*:*');
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
On line 19 io.set('origins', '*:*');I tried replacing ':' with '*', 'https://c9.io', 'c9.io','https://c9.io/' and '.', some times when I add something like 'c9.io/'
I get warn 'illegal origin ...' but that is just a cloud9 related issue.
Client code:
<html>
<body>
<script src="https://raw.github.com/LearnBoost/socket.io-client/master/dist/socket.io.js"></script>
<script>
var socket = io.connect('http://handsonwithnodejs.samarthwiz.c9.io');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</body>
</html>
I know that using github to get my script isn't the best idea yet, but I wanted to keep my code clean and the error messages readable(everything in 'socket.io.min.js' is on line 2)
P.S. 1. I know that there are other threads like this but they didn't solve my problem.
2. Please don't reply 'Just host the page on the same server as socket.io' I need it to be cross-domain for a reason.
I think you need to set the Access-Control-Allow-Origin header when you host the client code. Look at https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS for more info about the header.
This header must be set by the client for security reasons.
Browser can't find socket.io.js for client:
<script src="/socket.io/socket.io.js"></script>
When server is created without handler:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
app.listen(80);
//without this part:
/*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);
});
});
I don't need and don't want handler function because everything I generate in PHP. And sometimes use client application functions for another file than index.html/php
So how to make browser can find socket.io.js?
I've wrote a demo app that you could have a look at if you don't want to loose to much time getting started with socket.io and Express 3.
To have websockets working your client js needs to be delivered from a webserver. This is one of those many browser limitation.
The easiest setup is to have a node server that provide both the client side Js and the WebSockets. Using easier the http module of Express (a bit overkill but super practical if you want to build something more than just a test app).
Other wise you need to have your client side js pointing to the right place. For example if you run your socket.io server of port 8080 and you deliver your static client side on port 8000 (using python -m SimpleHTTPServer for example or port 80 using a regular apache).
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
If you don't need access to http module functionality use this way:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Include this on your client side !
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
var io = io.connect();