I have a error with nodejs 0.10.2 + express 3.x + socket.io 0.9.11

I have a error with nodejs 0.10.2 + express 3.x + socket.io 0.9.11

Problem

nodejs : v0.10.2 and express 3.x and socket.io 0.9.11

I just try sample code on the socket.io like lelow.

http.js708 throw new Error(can’t set headers after ther are sent)

http://socket.io/#how-to-use

Server side


    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);
      });
    });

client side – index.html


    
    
      var socket = io.connect('http://localhost');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    

than I have a error message like on the picture.

enter image description here

Problem courtesy of: user1736363

Solution

First u ‘ve to check the connection was established r not after that u continue ur code

here some sample code

var app = require('express')()
  , http = require('http')
  , server = http.createServer(app)
  , io = require('socket.io').listen(server)

server.listen(3000)    

io.on('connection',function(socket){
  console.log('connection..')
})
Solution courtesy of: AmirtharajCVijay

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *