title: Getting missing error handler socket message using Node.js and Socket.io
body: <p>Hi i am getting the following error which run the server using Node.js and socket.io.</p>
  
  <blockquote>
    <p>Error:</p>
  </blockquote>
  
  <pre><code>Missing error handler on `socket`.
  TypeError: Cannot read property 'connected' of undefined
      at Socket.&lt;anonymous&gt; (C:\Documents and Settings\Subhrajyoti\Desktop\Restore
  \htdocs\video broadcast\server.js:68:13)
      at Socket.emit (events.js:95:17)
      at Socket.onevent (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htd
  ocs\video broadcast\node_modules\socket.io\lib\socket.js:330:8)
      at Socket.onpacket (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\ht
  docs\video broadcast\node_modules\socket.io\lib\socket.js:290:12)
      at Client.ondecoded (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\h
  tdocs\video broadcast\node_modules\socket.io\lib\client.js:193:14)
      at Decoder.Emitter.emit (C:\Documents and Settings\Subhrajyoti\Desktop\Resto
  re\htdocs\video broadcast\node_modules\socket.io\node_modules\socket.io-parser\n
  ode_modules\component-emitter\index.js:134:20)
      at Decoder.add (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htdocs
  \video broadcast\node_modules\socket.io\node_modules\socket.io-parser\index.js:2
  47:12)
      at Client.ondata (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htdo
  cs\video broadcast\node_modules\socket.io\lib\client.js:175:18)
      at Socket.emit (events.js:95:17)
      at Socket.onPacket (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\ht
  docs\video broadcast\node_modules\socket.io\node_modules\engine.io\lib\socket.js
  :99:14)
  </code></pre>
  
  <p>My server side code is given below.</p>
  
  <blockquote>
    <p>Server.js:</p>
  </blockquote>
  
  <pre><code>var port=8888;
  var express=require('express');
  var morgan = require('morgan');
  var http=require('http');
  var bodyParser= require('body-parser');
  var methodOverride = require('method-override');
  var mongo = require('mongojs');
  var database='Oditek';
  var collections=['video'];
  var app= express();
  var server=http.Server(app);
  var io=require('socket.io')(server);
  var db = mongo.connect("127.0.0.1:27017/"+database, collections);
  app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users
  app.use(morgan('dev'));                     // log every request to the console
  app.use(bodyParser.urlencoded({ extended: false }))    // parse application/x-www-form-urlencoded
  app.use(bodyParser.json())    // parse application/json
  app.use(methodOverride());                  // simulate DELETE and PUT
  db.on('ready', function () {
      console.log('database connected')
  });
  app.get('/',function(req,res){
      res.sendfile('view/login.html');
  });
  app.post('/login',function(req,res){
      var username=req.body.username;
      var password=req.body.userpassword;
      if(username &amp;&amp; password){
          db.video.findOne({
              username:username,
              password:password
          },function(err,doc){
              if(doc){
                  console.log('login',doc);
                  res.send(doc);
              }
              if(err){
                  console.log('login12',err);
                  res.send("could not login");
              }
          });
      }
  });
  app.get('/video',function(req,res){
      res.sendfile('view/video.html');
  });
  
  //socket----programming//
  var roomid;
  var clients={};
  io.sockets.on('connection',function(socket){
      //console.log(socket);
      roomid=socket.handshake.query.roomid;
      var usertype=socket.handshake.query.usertype;
      socket.on('admin-join',function(data){
          console.log('admin',data);
          if(data.IsJoinAdmin){
              clients={
                  "socket":roomid
              }
              socket.join(roomid);
          }
      });
      socket.on('user-join',function(data){
          console.log('user',data);
          if(data.isUserJoin){
              io.socket.connected[clients.socket].emit('user-already-joined',data);
              socket.join(roomid);
          }
      });
      socket.on('send-broadcasting-message',function(data){
          console.log('message',data)
          io.to(roomid).emit('sending-broadcasting',data);
      });
  
  });
  server.listen(port);
  console.log('server is listening on the port'+port);
  </code></pre>
  
  <p>I am getting error at this line <code>io.socket.connected[clients.socket].emit('user-already-joined',data);</code>.This line i am using to send private message to a particular user.Please help me to resolve this error.</p>