I have two separate files that one is server-side JS.
The other one is dynamically generated client-side PHP.
Those two files are successfully able to communicate each other through Socket.IO.
I understand that I can restrict namespace by using .of() but that cannot be used
to handle dynamically created chat rooms.
So I have decided to use both
.of('/chat')
and the room feature
.join('room name')
I could find server-side example and could not find client-side example with it.
Below is the only given server-side code snippet from Socket.IO github
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.join('justin bieber fans');
socket.broadcast.to('justin bieber fans').emit('new fan');
io.sockets.in('rammstein fans').emit('new non-fan');
});
socket.broadcast.to('justin bieber fans').emit('new fan');
io.sockets.in('rammstein fans').emit('new non-fan');
What is difference between those two?
socket.to('room name').emit('event')
instead of
io.sockets.in('room name').emit('new non-fan');
.send()
instead of
.emit()
Somewhat .send() does not work for me and I want to know difference between those two.
Thanks and I apologize for multiple questions about Socket.IO.
1)-
i) io.sockets.in().emit(): It emit/dispatch a custom event to all in x room. Ex.;
/* Send message to the room1. It broadcasts the data to all
the socket clients which are connected to the room1 */
io.sockets.in('room1').emit('function', {foo:bar
});
ii) socket.broadcast.to().emit(): I emit/dispatch a custom event to all except the sender in x room. Ex.;
io.sockets.on('connection', function (socket) {
socket.on('function', function(data){
/* Broadcast to room1 except the sender. In other word,
It broadcast all the socket clients which are connected
to the room1 except the sender */
socket.broadcast.to('room1').emit('function', {foo:bar});
}
}
2)- First one, you emit/dispatch a custom event to the single client (socket one). Second one, you emit/dispatch a custom event to all the clients in x room.
3)- Different between emit() and send();
socket.emit(): is a function which emit/dispatch a custom event with a data to whoever clients you want to. socket.emit() takes at least two parameters, first one is the custom event name, second and so on ones are the data you want to pass. Ex.;
socket.emit('addUser',{nickname:'John'});
And you probably need to register and listen this custom event by using socket.on(). Ex.:
socket.on('addUser',function(data){
console.log(data.nickname); // it will return 'John'
}
socket.send(): is pretty much same as socket.emit() just this time it uses a default event name 'message'. so it takes just one parameter, data you want to pass. Ex.:
socket.send('hi');
And this time you register and listen the 'message' event name;
socket.on('message', function (data) {
console.log(data); // it will return 'hi'
})
Hope this will help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With