ctitze
ctitze

Reputation: 691

Socket.IO: Push to group of clients

I'm using a Node.js server with Express and want to do the following:

How can this be done in Socket.IO? (I have already read about "rooms" but I don't know how to use them together with a URL)

Upvotes: 2

Views: 877

Answers (1)

ctitze
ctitze

Reputation: 691

After experimenting a little bit with .handshake I came to a solution similar to that suggested by GeoPhoenix.
I don't know how good my solution is, but it works. Here's the code:

io.sockets.on('connection', function (socket) {
  var ref = JSON.stringify(socket.handshake.headers.referer);
  socket.join(ref);
  io.sockets.in(ref).emit('news', {hello: "world"});
});

As soon as a client connects the URL he used will be saved into ref and he joins a room named after that URL (of course the URL could be processed further to have better room-names, but that's not necessary for the solution to work).

Upvotes: 2

Related Questions