Skip to content

Commit 3323cbe

Browse files
authored
Ensure peer with requested peer name is disconnected (#139)
## Description Ensures a peer with requested peer name is disconnected. ## Motivation and Context This resolves the issue of peer name collision by returning a 409 when user asks for a taken peer name. ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
1 parent e0144e5 commit 3323cbe

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

examples/room-manager/src/plugins/fishjam.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fastifyPlugin from 'fastify-plugin';
22
import { type FastifyInstance } from 'fastify';
33
import {
44
FishjamClient,
5+
PeerId,
56
Room,
67
RoomConfigRoomTypeEnum,
78
RoomId,
@@ -58,6 +59,8 @@ export const fishjamPlugin = fastifyPlugin(async (fastify: FastifyInstance): Pro
5859
return createPeer(roomName, peerName);
5960
}
6061

62+
await ensurePeerIsDisconnected(room.id, peer.id);
63+
6164
if (!peerAccess?.peerToken) throw new RoomManagerError('Missing peer token in room');
6265

6366
peerAccess.peerToken = await fishjamClient.refreshPeerToken(room.id, peer.id);
@@ -172,6 +175,15 @@ export const fishjamPlugin = fastifyPlugin(async (fastify: FastifyInstance): Pro
172175
return newRoom;
173176
}
174177

178+
async function ensurePeerIsDisconnected(roomId: RoomId, peerId: PeerId) {
179+
const room = await fishjamClient.getRoom(roomId);
180+
const isPeerConnected = room.peers.some((peer) => peer.id === peerId && peer.status === 'connected');
181+
182+
if (isPeerConnected) {
183+
throw new RoomManagerError('Peer is already connected', 409);
184+
}
185+
}
186+
175187
function getBroadcastViewerToken(roomName: string) {
176188
const roomId = roomNameToRoomIdMap.get(roomName);
177189
if (!roomId) throw new RoomManagerError('Room not found', 404);

0 commit comments

Comments
 (0)