Getting Error Handler Error Messages Using Node.js and Socket.io

Hi, I am getting the following error that starts the server using Node.js and socket.io.

Error:

Missing error handler on `socket`. TypeError: Cannot read property 'connected' of undefined at Socket.<anonymous> (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) 

Below is the code for my server side.

Server.js:

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

I get an error in this line io.socket.connected[clients.socket].emit('user-already-joined',data); . I use this line to send a private message to a specific user. Please help me resolve this error.

+4
source share
2 answers

Your mistake was chosen because the io object does not have a property socket, but it has a sockets property. Therefore, you should write the following:

 io.sockets.connected[clients.socket].emit('user-already-joined',data); 

If you are using socket.io 1.0, this is the best way to do this. Each socket automatically joins the room by default using the identifier self.

Thus, you can emit a socket by id with the following code:

 io.to(clients.socket).emit('user-already-joined',data); 
0
source

Look at line 68 on server.js and comment on it, you will see that you are using the formula defined in the socket area.

0
source

All Articles