@@ -3,9 +3,10 @@ import { connect, socket, DataProps, MessageInfoProps } from "./socket";
33import { CommtContext } from "../context/Context" ;
44import * as types from "./emitTypes" ;
55import { updateUserOnline } from "../context/actions/usersActions" ;
6- import { addMessage } from "../context/actions/messagesAction" ;
6+ import { addMessage , deleteMessages } from "../context/actions/messagesAction" ;
77import {
88 addRoom ,
9+ deleteRoom ,
910 updateLastMessage ,
1011 updateReadToken ,
1112} from "../context/actions/roomsActions" ;
@@ -21,15 +22,7 @@ const SocketController = () => {
2122 rooms,
2223 users : { selfUser, users } ,
2324 app : {
24- configs : {
25- indicators,
26- tenantId,
27- apiKey,
28- subscriptionKey,
29- secretKey,
30- projectName,
31- e2e,
32- } ,
25+ configs : { indicators, tenantId, apiKey, projectId, secretKey, e2e } ,
3326 } ,
3427 } ,
3528 dispatch,
@@ -41,13 +34,13 @@ const SocketController = () => {
4134 connect ( {
4235 chatAuthId : selfUser ?. chatAuthId ,
4336 tenantId,
44- auth : { apiKey, subscriptionKey } ,
37+ projectId,
38+ auth : { apiKey } ,
4539 } ) ;
4640 } catch ( error ) {
4741 handleLogger ( {
4842 apiKey,
49- subscriptionKey,
50- projectName,
43+ projectId,
5144 chatAuthId : selfUser ?. chatAuthId ,
5245 error : {
5346 error,
@@ -66,8 +59,7 @@ const SocketController = () => {
6659 socket . on ( types . CONNECT_ERROR , ( error ) => {
6760 handleLogger ( {
6861 apiKey,
69- subscriptionKey,
70- projectName,
62+ projectId,
7163 chatAuthId : selfUser ?. chatAuthId ,
7264 error : {
7365 error,
@@ -87,8 +79,7 @@ const SocketController = () => {
8779 } catch ( error ) {
8880 handleLogger ( {
8981 apiKey,
90- subscriptionKey,
91- projectName,
82+ projectId,
9283 chatAuthId : selfUser ?. chatAuthId ,
9384 error : {
9485 error,
@@ -113,8 +104,7 @@ const SocketController = () => {
113104 } catch ( error ) {
114105 handleLogger ( {
115106 apiKey,
116- subscriptionKey,
117- projectName,
107+ projectId,
118108 chatAuthId : selfUser ?. chatAuthId ,
119109 error : {
120110 error,
@@ -134,8 +124,7 @@ const SocketController = () => {
134124 } catch ( error ) {
135125 handleLogger ( {
136126 apiKey,
137- subscriptionKey,
138- projectName,
127+ projectId,
139128 chatAuthId : selfUser ?. chatAuthId ,
140129 error : {
141130 error,
@@ -155,8 +144,7 @@ const SocketController = () => {
155144 } catch ( error ) {
156145 handleLogger ( {
157146 apiKey,
158- subscriptionKey,
159- projectName,
147+ projectId,
160148 chatAuthId : selfUser ?. chatAuthId ,
161149 chatRoomAuthId : data . chatRoomAuthId ,
162150 error : {
@@ -177,8 +165,7 @@ const SocketController = () => {
177165 } catch ( error ) {
178166 handleLogger ( {
179167 apiKey,
180- subscriptionKey,
181- projectName,
168+ projectId,
182169 chatAuthId : selfUser ?. chatAuthId ,
183170 chatRoomAuthId : data . chatRoomAuthId ,
184171 error : {
@@ -198,8 +185,7 @@ const SocketController = () => {
198185 } catch ( error ) {
199186 handleLogger ( {
200187 apiKey,
201- subscriptionKey,
202- projectName,
188+ projectId,
203189 chatAuthId : selfUser ?. chatAuthId ,
204190 chatRoomAuthId : data . room . chatRoomAuthId ,
205191 error : {
@@ -211,6 +197,37 @@ const SocketController = () => {
211197 } ) ;
212198 } , [ ] ) ;
213199
200+ useEffect ( ( ) => {
201+ socket . on ( types . UNSUBSCRIBE_ROOM , ( chatRoomAuthId ) => {
202+ try {
203+ // Send request to server to leave room
204+ socket . emit ( types . LEAVE_ROOM , chatRoomAuthId , ( { status } ) => {
205+ // Check if the socket successfully leaves the room
206+ if ( status === "success" ) {
207+ const roomId = rooms . find (
208+ ( room ) => room . chatRoomAuthId === chatRoomAuthId ,
209+ ) ?. roomId ;
210+
211+ // Update the context
212+ deleteRoom ( chatRoomAuthId ) ( dispatch ) ;
213+ roomId && deleteMessages ( roomId ) ( dispatch ) ;
214+ }
215+ } ) ;
216+ } catch ( error ) {
217+ handleLogger ( {
218+ apiKey,
219+ projectId,
220+ chatAuthId : selfUser ?. chatAuthId ,
221+ chatRoomAuthId : chatRoomAuthId ,
222+ error : {
223+ error,
224+ event : types . UNSUBSCRIBE_ROOM ,
225+ } ,
226+ } ) ;
227+ }
228+ } ) ;
229+ } , [ rooms ] ) ;
230+
214231 const handleMessage = ( data : DataProps ) => {
215232 try {
216233 const { message : messageData , iv } = data ;
@@ -271,8 +288,7 @@ const SocketController = () => {
271288 } catch ( error ) {
272289 handleLogger ( {
273290 apiKey,
274- subscriptionKey,
275- projectName,
291+ projectId,
276292 chatAuthId : selfUser ?. chatAuthId ,
277293 error : {
278294 error,
0 commit comments