message: Subscribing to messages when connecting to another peer
diff: @@ -44,10 +44,7 @@ export class Peer implements IPeer {
  .find(room => room.id === conn.label)
  ?.users.push({ id: conn.peer });
  - conn.on("data", data => {
  - console.log(data);
  - this.callback(conn.peer, data.room, data.payload);
  - });
  + this.subscribeToConnection(conn);
  conn.on("close", () => {
  // remove peer from channels and peer list
  @@ -56,6 +53,13 @@ export class Peer implements IPeer {
  });
  }
  + private subscribeToConnection(conn: PeerJS.DataConnection) {
  + conn.on("data", data => {
  + console.log(data);
  + this.callback(conn.peer, data.room, data.payload);
  + });
  + }
  +
  async joinRoom(roomId: string): Promise<void> {
  const room = await fetch(`${this.lighthouseUrl}/rooms/${roomId}`, {
  method: "PUT",
  @@ -76,6 +80,8 @@ export class Peer implements IPeer {
  console.log(`connection open to ${user.id}`);
  this.peers.push({ id: user.id, connection: conn });
  });
  +
  + this.subscribeToConnection(conn);
  });
  }
  