最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending message to specific group of clients in socketio - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 10

I 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');
});
发布评论

评论列表(0)

  1. 暂无评论