socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

io.to(testSocketId).emit('updatedDialog', 'you and I') is not working

Open bsayat opened this issue 7 years ago • 5 comments

Hi! I think there is a BUG! I cant send private message to the user with some id.

server.js


		socket.on('updateDialog', data => {
			console.log('receiver:     ', data.receiverId);
			let details = {"id": data.id};
			let chats = {id: data.id, dialog: data.dialog};
			
			// let socketID = "5bd04d61a9f6ddd0cc23e51b";
			
			// console.log(someTestData);
			
			database.collection('onlineUsers').findOne({"userId": data.receiverId}, (err, item)=> {
				if (err) {
					res.send(err);
				}
				testSocketId = item.socketId;
				if(data.id) {
				database.collection('chats').update(details, chats, (err, item)=>{
					if (err){
						console.log('error');
					} else {
						console.log('testSocketId:         ', testSocketId);
						io.to(testSocketId).emit('updatedDialog', 'you and I');
					}
				});
				} else {
					console.log('error');
				}
			});
		});

client.ts


this._chatService.socket.on('updatedDialog', (data) => { console.log(data); this.readyToReply(); });


If i replace the io.to(testSocketId).emit('updatedDialog', 'you and I') with io.to(socket.id).emit('updatedDialog', 'you and I') it works pretty well(message 'you and I' appears on my console). My socket.io version: "socket.io": "^2.2.0"

bsayat avatar Dec 13 '18 11:12 bsayat

Still the same till this day, had to use io.to(${someId}) like so or make an obj like const socket = { id: someId } and then io.to(socket.id)

JoseGabriel-web avatar Mar 26 '21 05:03 JoseGabriel-web

forgot the backtics io.to(${someId}) ...

JoseGabriel-web avatar Mar 26 '21 05:03 JoseGabriel-web

@JoseGabriel-web I tried using a template string like that, but it still didn't work, could you maybe give an example?

om-ray avatar Apr 07 '21 18:04 om-ray

I have tried io.to(socket.id).emit(), I have even tried every other one, it is =still not working

blackgik avatar Dec 24 '21 14:12 blackgik

@blackgik are you able to reproduce with a basic example? (you can fork the project here)

darrachequesne avatar Dec 28 '21 09:12 darrachequesne