Q:

Broadcast or peer discovery with PeerJS

I am evaluating PeerJS in order to implement a simple two-player online game. It seems that once I transfer the id of one player’s connection to the other, they can open a channel via PeerJS and are good to go.
But if two players want to play that do not know each other, what is the most elegant way to make a match there? Is there a way to ask the PeerJS broker for a list of all connected clients, possibly with some metadata (such as „status:wants-to-play”) attached? Or is ther ea way to broadcast to all clients?

A:

Using PeerServer you can grab two events, connection and disconnect. Using this you can create a internal list, which you can then have your application grab from.
Partial example:
var PeerServer = require('peer').PeerServer;
var server = new PeerServer({port: 9000, path: '/myapp'});
var connected = [];
server.on('connection', function (id) {
  var idx = connected.indexOf(id); // only add id if it's not in the list yet
  if (idx === -1) {connected.push(id);}
});
server.on('disconnect', function (id) {
  var idx = connected.indexOf(id); // only attempt to remove id if it's in the list
  if (idx !== -1) {connected.splice(idx, 1);}
});
someexpressapp.get('/connected-people', function (req, res) {
  return res.json(connected);
});

Then, in your clientside code you can AJAX /connected-people and use that list.
For metadata you could expand on the code above to add a user status and a way of updating that status.
Hope this helps!
EDIT At the time of writing the event was named connect. It is now named connection.
(Also I'm now going to play with PeerJS for like six hours. I hope you realize what you've done.)

