2

I'm trying to develop a simple chat application. Here is my chat.js file.

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
app.listen(8124);
function handler (req, res) {
fs.readFile(__dirname + '/chat.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading chat.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
    socket.on('addme',function(username) {
        socket.username = username;
        socket.emit('chat', 'SERVER', 'You have connected');
        socket.broadcast.emit('chat', 'SERVER', username + ' is on deck');
    });
    socket.on('sendchat', function(data) {
        io.sockets.emit('chat', socket.username, data);
    });
    socket.on('disconnect', function() {
        io.sockets.emit('chat', 'SERVER', socket.username + ' has left the building');
    });
});

And my chat.html file.

<head>
<meta charset="utf-8">
<title>bi-directional communication</title>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
    var socket = io.connect('http://localhost:8124/');
    $('#submit').click(function(e) {
        e.preventDefault();
        v = $('#uname').val();
        $('#username').html('');
        socket.emit('addme', v);
    });

    socket.on('chat',function(username, data) {
        var p = document.createElement('p');
        p.innerHTML = username + ': ' + data;
        document.getElementById('output').appendChild(p);
    });
    window.addEventListener('load',function() {
        document.getElementById('sendtext').addEventListener('click',
        function() {
            var text = document.getElementById('data').value;
            socket.emit('sendchat', text);
        }, false);
    }, false);

});
</script>
</head>
<body>
<div id="output"></div>
<div id="username">
  <input type="text" name="uname" id="uname">
  <input type="submit" name="submit" id="submit" value="Submit">
</div>
<div id="send">
  <input type="text" id="data" size="100" /><br />
<input type="button" id="sendtext" value="Send Text" />
</div>
</body>
</html>

I test the code by typing node chat.js in node.js command prompt & then typing http://localhost:8124/ in my browser address bar. The problem is that while this works perfectly on IE9, nothing happens on Firefox and Chrome.

I'm receiving the following on Node.js command prompt when I run this on chrome or firefox.

info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized 0yJjYvt7o36BTX6T_hXA
   debug - setting request GET /socket.io/1/websocket/0yJjYvt7o36BTX6T_hXA
   debug - set heartbeat interval for client 0yJjYvt7o36BTX6T_hXA
   debug - client authorized for
   debug - websocket writing 1::
   debug - setting request GET /socket.io/1/xhr-polling/0yJjYvt7o36BTX6T_hXA?t=1
341573194770
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared heartbeat interval for client 0yJjYvt7o36BTX6T_hXA
   debug - setting request GET /socket.io/1/jsonp-polling/0yJjYvt7o36BTX6T_hXA?t
=1341573204771&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - clearing poll timeout
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client 0yJjYvt7o36BTX6T_hXA
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/0yJjYvt7o36BTX6T_hXA?t
=1341573224817&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client 0yJjYvt7o36BTX6T_hXA
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client 0yJjYvt7o36BTX6T_hXA
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/0yJjYvt7o36BTX6T_hXA?t
=1341573244856&i=0

and this goes on. Please help.

2
  • Seems to work for me just fine (node 0.8.0, socket.io 0.9.6, Windows XP). One possibility: unwrap the client stuff inside the "load" event listener; depending on timing, that event might already have fired before jQuery's ready handler and be missed.
    – ebohlman
    Jul 5, 2012 at 19:09
  • But why should it make any difference when it works perfectly on IE?
    – Shekhar
    Jul 6, 2012 at 10:01

3 Answers 3

5

Thankyou ebohlman for your time but I solved the problem. In my chat.js I added the following line.

io.configure('development', function(){
  io.set('transports', ['xhr-polling']);
});

Now my chat.js looks like.

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');

app.listen(8124);

io.configure('development', function(){
  io.set('transports', ['xhr-polling']);
});

function handler (req, res) {
fs.readFile(__dirname + '/chat.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading chat.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
    socket.on('addme',function(username) {
        socket.username = username;
        socket.emit('chat', 'SERVER', 'You have connected');
        socket.broadcast.emit('chat', 'SERVER', username + ' is on deck');
    });
    socket.on('sendchat', function(data) {
        io.sockets.emit('chat', socket.username, data);
    });
    socket.on('disconnect', function() {
        io.sockets.emit('chat', 'SERVER', socket.username + ' has left the building');
    });
});

But I still don't know what might have caused the error. If you know please explain!

3
  • +1 because that line at least made it working in Google Chrome for me. I was just banging my head since an hour to make that work. Thanks for posting this :) Apr 28, 2013 at 22:32
  • For some reason it doesn't work in IE and Mozilla, in case anybody has any solutions please post, Thanks.. Apr 28, 2013 at 22:40
  • Thanks for posting this answer.. it was a headache for me too. :) Jan 23, 2015 at 4:30
1

I encountered the same issues when trying to publish to AppFog. It's caused by websockets not being supported by AppFog yet, so your fix worked by forcing socket.io to use long polling/AJAX. AppFog is working on supporting websockets according to their website, but no dates have been given.

Most cloud PaaS providers that I have found do NOT support websockets yet unfortunately. One notable exception that I am testing out now is Nodejitsu: http://nodejitsu.com/

0

I think this will help to know about the issue, and how it fixes I searched lot for it and finally I got it

What does "xhr-polling" config do in socket.io?

thank you

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.