What's the best way to send a message via socket io to a group of users, for example only those in a specific chat room instead of all users?
I am aware of Sending data only to chosen users using Socket.io-node but socketio version .7 was released recently and seems like there might be a more elegant way with the new api using either get/set or namespacing?
What's the best way to send a message via socket io to a group of users, for example only those in a specific chat room instead of all users?
I am aware of Sending data only to chosen users using Socket.io-node but socketio version .7 was released recently and seems like there might be a more elegant way with the new api using either get/set or namespacing?
Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked Jul 29, 2011 at 22:54 jhchenjhchen 14.8k14 gold badges65 silver badges91 bronze badges1 Answer
Reset to default 10I think you should look up room concept:
Rooms
Sometimes you want to put certain sockets in the same room, so that it's easy to broadcast to all of them together.
Think of this as built-in channels for sockets. Sockets join and leave rooms in each socket.
Server side:
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'); });