3

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"

Guido Passage
  • 1,040
  • 1
  • 10
  • 15
  • Any errors or logs getting displayed on console [server side] by node ? – Pranav Jan 10 '14 at 11:28
  • No, everything seems to be going fine server side. The 'my other event' events are also received properly (in batches of 13 if the client receives a batch of 13 'news' events) – Guido Passage Jan 10 '14 at 11:56
  • Node 0.8 is pretty old, perhaps time to update to 0.10? – robertklep Jan 10 '14 at 12:25
  • I am using 0.8 on purpose. 0.10 gives trouble when I want to use socket.io with node-http-proxy, see http://stackoverflow.com/questions/20146720/socket-io-example-sometimes-not-connecting-client-side-when-using-a-reverse-prox – Guido Passage Jan 10 '14 at 12:32
  • Does that mean you have a proxy in between the client and the server? – Kurt Pattyn Jan 12 '14 at 21:05
  • Yes, but I get the same problems if I run it directly. – Guido Passage Jan 12 '14 at 21:16
  • opcode 11 is reserved opcode by WebSockets protocol, and should not be sent to browser in first place. Looks like just a bug within socket.io, that potentially did wrong recognising WS version of browser, and though it can send it, but suddenly no. – moka Mar 06 '14 at 17:57
  • Thank you, that could exactly be the problem. Does anyone know if there is a way to work around this, except for using xhr-polling instead of websockets? – Guido Passage Mar 09 '14 at 12:59

0 Answers0