@@ -11,6 +11,12 @@ import {
11
11
interface Props {
12
12
deviceName : string ; // Unique name to identify the device
13
13
socketURL : string ; // Base URL of the socket server (may be modified based on platform)
14
+ /**
15
+ * When true, enables console logging of socket operations
16
+ * Set to false to silence logs in production environments
17
+ * @default false
18
+ */
19
+ enableDebugLogs ?: boolean ;
14
20
}
15
21
16
22
// Key for storing the persistent device ID in AsyncStorage
@@ -35,7 +41,9 @@ const generateDeviceId = (): string => {
35
41
/**
36
42
* Gets or creates a persistent device ID
37
43
*/
38
- const getOrCreateDeviceId = async ( ) : Promise < string > => {
44
+ const getOrCreateDeviceId = async (
45
+ enableDebugLogs = false
46
+ ) : Promise < string > => {
39
47
try {
40
48
// Check if we already have the ID in memory
41
49
if ( deviceId ) {
@@ -59,7 +67,9 @@ const getOrCreateDeviceId = async (): Promise<string> => {
59
67
deviceId = newId ;
60
68
return newId ;
61
69
} catch ( error ) {
62
- console . error ( "Failed to get/create device ID:" , error ) ;
70
+ if ( enableDebugLogs ) {
71
+ console . error ( "Failed to get/create device ID:" , error ) ;
72
+ }
63
73
// Fallback to a temporary ID if storage fails
64
74
const tempId = generateDeviceId ( ) ;
65
75
deviceId = tempId ;
@@ -77,7 +87,11 @@ const getOrCreateDeviceId = async (): Promise<string> => {
77
87
* - Connection state tracking
78
88
* - User list management
79
89
*/
80
- export function useMySocket ( { deviceName, socketURL } : Props ) {
90
+ export function useMySocket ( {
91
+ deviceName,
92
+ socketURL,
93
+ enableDebugLogs = false ,
94
+ } : Props ) {
81
95
const socketRef = useRef < Socket | null > ( null ) ;
82
96
const [ socket , setSocket ] = useState < Socket | null > ( null ) ;
83
97
const [ isConnected , setIsConnected ] = useState ( false ) ;
@@ -90,46 +104,60 @@ export function useMySocket({ deviceName, socketURL }: Props) {
90
104
// For logging clarity
91
105
const logPrefix = `[${ deviceName } ]` ;
92
106
107
+ // Utility function for conditional debug logging
108
+ const debugLog = ( message : string , ...args : any [ ] ) => {
109
+ if ( enableDebugLogs ) {
110
+ console . log ( message , ...args ) ;
111
+ }
112
+ } ;
113
+
114
+ // Utility function for conditional error logging
115
+ const debugError = ( message : string , ...args : any [ ] ) => {
116
+ if ( enableDebugLogs ) {
117
+ console . error ( message , ...args ) ;
118
+ }
119
+ } ;
120
+
93
121
// Get the current platform
94
122
const { name : currentPlatform } = getPlatform ( ) ;
95
123
96
124
// Define event handlers at function root level to satisfy linter
97
125
const onConnect = ( ) => {
98
- console . log ( `${ logPrefix } Socket connected successfully` ) ;
126
+ debugLog ( `${ logPrefix } Socket connected successfully` ) ;
99
127
setIsConnected ( true ) ;
100
128
} ;
101
129
102
130
const onDisconnect = ( reason : string ) => {
103
- console . log ( `${ logPrefix } Socket disconnected. Reason: ${ reason } ` ) ;
131
+ debugLog ( `${ logPrefix } Socket disconnected. Reason: ${ reason } ` ) ;
104
132
setIsConnected ( false ) ;
105
133
} ;
106
134
107
135
const onUsersUpdate = ( newUsers : User [ ] ) => {
108
- console . log (
136
+ debugLog (
109
137
`${ logPrefix } Users updated:` ,
110
138
newUsers . map ( ( u ) => u . deviceName ) . join ( ", " )
111
139
) ;
112
140
setUsers ( newUsers ) ;
113
141
} ;
114
142
115
143
const onConnectError = ( error : Error ) => {
116
- console . error ( `${ logPrefix } Socket connection error:` , error . message ) ;
144
+ debugError ( `${ logPrefix } Socket connection error:` , error . message ) ;
117
145
} ;
118
146
119
147
const onConnectTimeout = ( ) => {
120
- console . error ( `${ logPrefix } Socket connection timeout` ) ;
148
+ debugError ( `${ logPrefix } Socket connection timeout` ) ;
121
149
} ;
122
150
123
151
// Get persistent device ID
124
152
useEffect ( ( ) => {
125
153
const fetchDeviceId = async ( ) => {
126
- const id = await getOrCreateDeviceId ( ) ;
154
+ const id = await getOrCreateDeviceId ( enableDebugLogs ) ;
127
155
setPersistentDeviceId ( id ) ;
128
- console . log ( `${ logPrefix } Using persistent device ID: ${ id } ` ) ;
156
+ debugLog ( `${ logPrefix } Using persistent device ID: ${ id } ` ) ;
129
157
} ;
130
158
131
159
fetchDeviceId ( ) ;
132
- } , [ logPrefix ] ) ;
160
+ } , [ logPrefix , enableDebugLogs ] ) ;
133
161
134
162
// Main socket initialization - runs only once
135
163
useEffect ( ( ) => {
@@ -149,16 +177,14 @@ export function useMySocket({ deviceName, socketURL }: Props) {
149
177
const platformUrl = getPlatformSpecificURL ( socketURL ) ;
150
178
currentSocketURL = platformUrl ;
151
179
152
- console . log (
180
+ debugLog (
153
181
`${ logPrefix } Platform: ${ currentPlatform } , using URL: ${ platformUrl } `
154
182
) ;
155
183
156
184
try {
157
185
// Use existing global socket or create a new one
158
186
if ( ! globalSocketInstance ) {
159
- console . log (
160
- `${ logPrefix } Creating new socket instance to ${ platformUrl } `
161
- ) ;
187
+ debugLog ( `${ logPrefix } Creating new socket instance to ${ platformUrl } ` ) ;
162
188
globalSocketInstance = socketIO ( platformUrl , {
163
189
autoConnect : true ,
164
190
query : {
@@ -170,7 +196,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
170
196
transports : [ "websocket" ] , // Prefer websocket transport for React Native
171
197
} ) ;
172
198
} else {
173
- console . log (
199
+ debugLog (
174
200
`${ logPrefix } Reusing existing socket instance to ${ platformUrl } `
175
201
) ;
176
202
}
@@ -185,7 +211,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
185
211
// Check initial connection state
186
212
if ( socketRef . current . connected ) {
187
213
setIsConnected ( true ) ;
188
- console . log ( `${ logPrefix } Socket already connected on init` ) ;
214
+ debugLog ( `${ logPrefix } Socket already connected on init` ) ;
189
215
}
190
216
191
217
// Set up event handlers
@@ -198,7 +224,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
198
224
// Clean up event listeners on unmount but don't disconnect
199
225
return ( ) => {
200
226
if ( socketRef . current ) {
201
- console . log ( `${ logPrefix } Cleaning up socket event listeners` ) ;
227
+ debugLog ( `${ logPrefix } Cleaning up socket event listeners` ) ;
202
228
socketRef . current . off ( "connect" , onConnect ) ;
203
229
socketRef . current . off ( "disconnect" , onDisconnect ) ;
204
230
socketRef . current . off ( "connect_error" , onConnectError ) ;
@@ -209,10 +235,10 @@ export function useMySocket({ deviceName, socketURL }: Props) {
209
235
}
210
236
} ;
211
237
} catch ( error ) {
212
- console . error ( `${ logPrefix } Failed to initialize socket:` , error ) ;
238
+ debugError ( `${ logPrefix } Failed to initialize socket:` , error ) ;
213
239
}
214
240
// eslint-disable-next-line react-hooks/exhaustive-deps
215
- } , [ persistentDeviceId ] ) ;
241
+ } , [ persistentDeviceId , enableDebugLogs ] ) ;
216
242
217
243
// Update the socket query parameters when deviceName changes
218
244
useEffect ( ( ) => {
@@ -221,15 +247,21 @@ export function useMySocket({ deviceName, socketURL }: Props) {
221
247
socketRef . current . io . opts . query &&
222
248
persistentDeviceId
223
249
) {
224
- console . log ( `${ logPrefix } Updating device name in socket connection` ) ;
250
+ debugLog ( `${ logPrefix } Updating device name in socket connection` ) ;
225
251
socketRef . current . io . opts . query = {
226
252
...socketRef . current . io . opts . query ,
227
253
deviceName,
228
254
deviceId : persistentDeviceId ,
229
255
platform : currentPlatform ,
230
256
} ;
231
257
}
232
- } , [ deviceName , logPrefix , persistentDeviceId , currentPlatform ] ) ;
258
+ } , [
259
+ deviceName ,
260
+ logPrefix ,
261
+ persistentDeviceId ,
262
+ currentPlatform ,
263
+ enableDebugLogs ,
264
+ ] ) ;
233
265
234
266
// Update the socket URL when socketURL changes
235
267
useEffect ( ( ) => {
@@ -242,7 +274,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
242
274
currentSocketURL !== platformUrl &&
243
275
persistentDeviceId
244
276
) {
245
- console . log (
277
+ debugLog (
246
278
`${ logPrefix } Socket URL changed from ${ currentSocketURL } to ${ platformUrl } `
247
279
) ;
248
280
@@ -251,7 +283,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
251
283
socketRef . current . disconnect ( ) ;
252
284
currentSocketURL = platformUrl ;
253
285
254
- console . log (
286
+ debugLog (
255
287
`${ logPrefix } Creating new socket connection to ${ platformUrl } `
256
288
) ;
257
289
globalSocketInstance = socketIO ( platformUrl , {
@@ -268,20 +300,24 @@ export function useMySocket({ deviceName, socketURL }: Props) {
268
300
socketRef . current = globalSocketInstance ;
269
301
setSocket ( socketRef . current ) ;
270
302
} catch ( error ) {
271
- console . error (
272
- `${ logPrefix } Failed to update socket connection:` ,
273
- error
274
- ) ;
303
+ debugError ( `${ logPrefix } Failed to update socket connection:` , error ) ;
275
304
}
276
305
}
277
- } , [ socketURL , deviceName , logPrefix , persistentDeviceId , currentPlatform ] ) ;
306
+ } , [
307
+ socketURL ,
308
+ deviceName ,
309
+ logPrefix ,
310
+ persistentDeviceId ,
311
+ currentPlatform ,
312
+ enableDebugLogs ,
313
+ ] ) ;
278
314
279
315
/**
280
316
* Manually connect to the socket server
281
317
*/
282
318
function connect ( ) {
283
319
if ( socketRef . current && ! socketRef . current . connected ) {
284
- console . log ( `${ logPrefix } Manually connecting to socket server` ) ;
320
+ debugLog ( `${ logPrefix } Manually connecting to socket server` ) ;
285
321
socketRef . current . connect ( ) ;
286
322
}
287
323
}
@@ -291,7 +327,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
291
327
*/
292
328
function disconnect ( ) {
293
329
if ( socketRef . current && socketRef . current . connected ) {
294
- console . log ( `${ logPrefix } Manually disconnecting from socket server` ) ;
330
+ debugLog ( `${ logPrefix } Manually disconnecting from socket server` ) ;
295
331
socketRef . current . disconnect ( ) ;
296
332
}
297
333
}
0 commit comments