Skip to content

Commit bd0ddcc

Browse files
committed
Added the socket listener and emit for unsubscribe logic
1 parent 3a59fe7 commit bd0ddcc

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/utils/SocketController.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { connect, socket, DataProps, MessageInfoProps } from "./socket";
33
import { CommtContext } from "../context/Context";
44
import * as types from "./emitTypes";
55
import { updateUserOnline } from "../context/actions/usersActions";
6-
import { addMessage } from "../context/actions/messagesAction";
6+
import { addMessage, deleteMessages } from "../context/actions/messagesAction";
77
import {
88
addRoom,
9+
deleteRoom,
910
updateLastMessage,
1011
updateReadToken,
1112
} from "../context/actions/roomsActions";
@@ -211,6 +212,38 @@ const SocketController = () => {
211212
});
212213
}, []);
213214

215+
useEffect(() => {
216+
socket.on(types.UNSUBSCRIBE_ROOM, (chatRoomAuthId) => {
217+
try {
218+
// Send request to server to leave room
219+
socket.emit(types.LEAVE_ROOM, chatRoomAuthId, ({ status }) => {
220+
// Check if the socket successfully leaves the room
221+
if (status === "success") {
222+
const roomId = rooms.find(
223+
(room) => room.chatRoomAuthId === chatRoomAuthId,
224+
)?.roomId;
225+
226+
// Update the context
227+
deleteRoom(chatRoomAuthId)(dispatch);
228+
roomId && deleteMessages(roomId)(dispatch);
229+
}
230+
});
231+
} catch (error) {
232+
handleLogger({
233+
apiKey,
234+
subscriptionKey,
235+
projectName,
236+
chatAuthId: selfUser?.chatAuthId,
237+
chatRoomAuthId: chatRoomAuthId,
238+
error: {
239+
error,
240+
event: types.UNSUBSCRIBE_ROOM,
241+
},
242+
});
243+
}
244+
});
245+
}, [rooms]);
246+
214247
const handleMessage = (data: DataProps) => {
215248
try {
216249
const { message: messageData, iv } = data;

0 commit comments

Comments
 (0)