Skip to content

Commit 6ecae96

Browse files
authored
Merge pull request #1046 from layer5io/roomActivity
expose collab websocket and add support for subscribing to rooms
2 parents dbf9276 + 39229eb commit 6ecae96

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/hooks/useRoomActivity.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef, useState } from 'react';
1+
import { MutableRefObject, useEffect, useRef, useState } from 'react';
22
import {
33
ICE_SERVERS,
44
ICEServer,
@@ -152,6 +152,7 @@ const subscribeToRoomActivity = async (
152152
});
153153

154154
ws.addEventListener('message', (event: MessageEvent) => {
155+
console.log('[RoomActivity] new message', event);
155156
const data = JSON.parse(event.data) as UserMapChangeMessage;
156157
if (data.type === USER_MAP_CHANGE_MSG && data.user_map) {
157158
onUserMapChange(data.user_map);
@@ -174,7 +175,7 @@ export const useRoomActivity = ({
174175
provider_url,
175176
getUserProfile,
176177
getUserAccessToken
177-
}: UseRoomActivityParams): UserMapping => {
178+
}: UseRoomActivityParams): [UserMapping, MutableRefObject<WebSocket | null>] => {
178179
const [allRoomsUserMapping, setAllRoomsUserMapping] = useState<UserMapping>({});
179180
const wsRef = useRef<WebSocket | null>(null);
180181

@@ -191,10 +192,29 @@ export const useRoomActivity = ({
191192

192193
return () => {
193194
if (ws) {
195+
console.log('closing websocket', ws);
194196
ws.close();
195197
}
196198
};
197199
}, [provider_url, getUserProfile, getUserAccessToken]);
198200

199-
return allRoomsUserMapping;
201+
return [allRoomsUserMapping, wsRef];
202+
};
203+
204+
export const subscribeToRoom = (ws: WebSocket, room: string) => {
205+
ws.send(
206+
JSON.stringify({
207+
type: 'subscribe',
208+
topic: room
209+
})
210+
);
211+
};
212+
213+
export const unSubscribeRoom = (ws: WebSocket, room: string) => {
214+
ws.send(
215+
JSON.stringify({
216+
type: 'unsubscribe',
217+
topic: room
218+
})
219+
);
200220
};

0 commit comments

Comments
 (0)