Wednesday 27 March 2019

How to send a private message to a particular socket ID ?

I have been trying to fiddle with this with no luck. With the code I provide below I am able to connect, retrieve the redis socket Id key and broadcast to all users, but I am unable to send a private message or rather, a message to a socket id; this is needed for private messaging as I really don't want to use rooms. The code block demonstrates the logic; I have hid a few things to just to target my question to free you from distractions after if (res ==1 ) is where the code is misbehaving in the private socket id message.​io.on('connection', function (socket) {console.log(' Connection established');socket.on('private', function (data) {let chat_object = JSON.parse(data.chat_object);let message = chat_object.text;let recipient = chat_object.to;let sender = chat_object.from;let date = chat_object.date;let read_flag = chat_object.read;let message_pending_for = recipient;let _active_users = new ActiveClient();_active_users.add_new_client(sender, socket.id);_active_users.does_client_exist(recipient).then(res => {if (res === 1) {_active_users.get_client_key(recipient).then(socket_id => {console.log('============================'+io.sockets.name)console.log('I am sending the message to: '+ recipient + 'and my socket id is'+ socket_id)io.sockets.to(socket_id).emit('incoming', "this is top secret")}).catch(error => {console.log("COULD NOT RETRIEVE KEY: " + error)})}}).catch(error => {console.log("Message Not Delivered To Recipient" + error)})})

Submitted March 28, 2019 at 01:14AM by Fuerzacode

No comments:

Post a Comment