11

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?

Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139

2 Answers2

26

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.)

Community
  • 1
  • 1
Zoey Mertes
  • 3,139
  • 19
  • 23
  • 6
    @ZekeSonxx `Object.keys( server._clients.peerjs );` the same array – Ivan Black Jul 03 '14 at 16:54
  • 5
    You can also pass the option `allow_discovery: true` when creating the peer server. Then just use the method `peer.listAllPeers` in the browser. I think this is not documented on the peerjs website. – sousa Mar 04 '15 at 15:10
  • I edited in `indexOf` checks, because network events are unreliable so it could be we miss one or two. With the added checks the event listeners become [idempotent](https://en.wikipedia.org/wiki/Idempotence). – Stijn de Witt Jul 07 '15 at 10:20
  • 2
    There is more info on peer.listAllPeers() here: https://github.com/peers/peerjs/issues/173 The method uses a callback: ```peer.listAllPeers((allPeers)=> { console.log(allPeers) })``` – Js. Sep 24 '20 at 21:16
  • I think listAllPeers works the purpose, but how do discover the peers who join later this call? can this peer server also push messages to the client? – Naga Oct 02 '22 at 19:41
3

Just in case, if this helps someone, if you use PeerJS in conjunction with express server, this would look the following way:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var ExpressPeerServer = require('peer').ExpressPeerServer;
var expressPeerServer = ExpressPeerServer(server, {});
app.use('/peerjs', expressPeerServer);
expressPeerServer.on('connection', function (id) {
  // the rest is the same as accepted answer
});
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206