How to send messages only to users with a specific ID?
For example, I have a user with id = 5 and I want to send a message only to him, not to all users connected to the socket. How can I send this id to the server?
Client
<?php $id=5; // id to send to echo ' <div id="id">'.$id.'</div> <div id="messages"></div> <input type="text" id="type"> <div id="btn">Press</div> '; ?> <script> $(document).ready(function(){ var id=$('#id').html(); var socket=io.connect('http://localhost:8010'); socket.on('connecting',function(){alert('Connecting');}); socket.on('connect',function(){alert('Connected');}); socket.on('message',function(data){message(data.text);}); function message(text){$('#messages').append(text+'<br>');} $('#btn').click(function(){ var text=$('#type').val(); socket.emit("message",{text:text}); }); }); </script> Server
io.sockets.on('connection',function(client){ client.on('message',function(message){ try{ client.emit('message',message); client.broadcast.emit('message', message); }catch(e){ console.log(e); client.disconnect(); } }); }); How to transfer id = 5 to the server?