@@ -11,6 +11,12 @@ import {
1111interface Props {
1212 deviceName : string ; // Unique name to identify the device
1313 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 ;
1420}
1521
1622// Key for storing the persistent device ID in AsyncStorage
@@ -35,7 +41,9 @@ const generateDeviceId = (): string => {
3541/**
3642 * Gets or creates a persistent device ID
3743 */
38- const getOrCreateDeviceId = async ( ) : Promise < string > => {
44+ const getOrCreateDeviceId = async (
45+ enableDebugLogs = false
46+ ) : Promise < string > => {
3947 try {
4048 // Check if we already have the ID in memory
4149 if ( deviceId ) {
@@ -59,7 +67,9 @@ const getOrCreateDeviceId = async (): Promise<string> => {
5967 deviceId = newId ;
6068 return newId ;
6169 } 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+ }
6373 // Fallback to a temporary ID if storage fails
6474 const tempId = generateDeviceId ( ) ;
6575 deviceId = tempId ;
@@ -77,7 +87,11 @@ const getOrCreateDeviceId = async (): Promise<string> => {
7787 * - Connection state tracking
7888 * - User list management
7989 */
80- export function useMySocket ( { deviceName, socketURL } : Props ) {
90+ export function useMySocket ( {
91+ deviceName,
92+ socketURL,
93+ enableDebugLogs = false ,
94+ } : Props ) {
8195 const socketRef = useRef < Socket | null > ( null ) ;
8296 const [ socket , setSocket ] = useState < Socket | null > ( null ) ;
8397 const [ isConnected , setIsConnected ] = useState ( false ) ;
@@ -90,46 +104,60 @@ export function useMySocket({ deviceName, socketURL }: Props) {
90104 // For logging clarity
91105 const logPrefix = `[${ deviceName } ]` ;
92106
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+
93121 // Get the current platform
94122 const { name : currentPlatform } = getPlatform ( ) ;
95123
96124 // Define event handlers at function root level to satisfy linter
97125 const onConnect = ( ) => {
98- console . log ( `${ logPrefix } Socket connected successfully` ) ;
126+ debugLog ( `${ logPrefix } Socket connected successfully` ) ;
99127 setIsConnected ( true ) ;
100128 } ;
101129
102130 const onDisconnect = ( reason : string ) => {
103- console . log ( `${ logPrefix } Socket disconnected. Reason: ${ reason } ` ) ;
131+ debugLog ( `${ logPrefix } Socket disconnected. Reason: ${ reason } ` ) ;
104132 setIsConnected ( false ) ;
105133 } ;
106134
107135 const onUsersUpdate = ( newUsers : User [ ] ) => {
108- console . log (
136+ debugLog (
109137 `${ logPrefix } Users updated:` ,
110138 newUsers . map ( ( u ) => u . deviceName ) . join ( ", " )
111139 ) ;
112140 setUsers ( newUsers ) ;
113141 } ;
114142
115143 const onConnectError = ( error : Error ) => {
116- console . error ( `${ logPrefix } Socket connection error:` , error . message ) ;
144+ debugError ( `${ logPrefix } Socket connection error:` , error . message ) ;
117145 } ;
118146
119147 const onConnectTimeout = ( ) => {
120- console . error ( `${ logPrefix } Socket connection timeout` ) ;
148+ debugError ( `${ logPrefix } Socket connection timeout` ) ;
121149 } ;
122150
123151 // Get persistent device ID
124152 useEffect ( ( ) => {
125153 const fetchDeviceId = async ( ) => {
126- const id = await getOrCreateDeviceId ( ) ;
154+ const id = await getOrCreateDeviceId ( enableDebugLogs ) ;
127155 setPersistentDeviceId ( id ) ;
128- console . log ( `${ logPrefix } Using persistent device ID: ${ id } ` ) ;
156+ debugLog ( `${ logPrefix } Using persistent device ID: ${ id } ` ) ;
129157 } ;
130158
131159 fetchDeviceId ( ) ;
132- } , [ logPrefix ] ) ;
160+ } , [ logPrefix , enableDebugLogs ] ) ;
133161
134162 // Main socket initialization - runs only once
135163 useEffect ( ( ) => {
@@ -149,16 +177,14 @@ export function useMySocket({ deviceName, socketURL }: Props) {
149177 const platformUrl = getPlatformSpecificURL ( socketURL ) ;
150178 currentSocketURL = platformUrl ;
151179
152- console . log (
180+ debugLog (
153181 `${ logPrefix } Platform: ${ currentPlatform } , using URL: ${ platformUrl } `
154182 ) ;
155183
156184 try {
157185 // Use existing global socket or create a new one
158186 if ( ! globalSocketInstance ) {
159- console . log (
160- `${ logPrefix } Creating new socket instance to ${ platformUrl } `
161- ) ;
187+ debugLog ( `${ logPrefix } Creating new socket instance to ${ platformUrl } ` ) ;
162188 globalSocketInstance = socketIO ( platformUrl , {
163189 autoConnect : true ,
164190 query : {
@@ -170,7 +196,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
170196 transports : [ "websocket" ] , // Prefer websocket transport for React Native
171197 } ) ;
172198 } else {
173- console . log (
199+ debugLog (
174200 `${ logPrefix } Reusing existing socket instance to ${ platformUrl } `
175201 ) ;
176202 }
@@ -185,7 +211,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
185211 // Check initial connection state
186212 if ( socketRef . current . connected ) {
187213 setIsConnected ( true ) ;
188- console . log ( `${ logPrefix } Socket already connected on init` ) ;
214+ debugLog ( `${ logPrefix } Socket already connected on init` ) ;
189215 }
190216
191217 // Set up event handlers
@@ -198,7 +224,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
198224 // Clean up event listeners on unmount but don't disconnect
199225 return ( ) => {
200226 if ( socketRef . current ) {
201- console . log ( `${ logPrefix } Cleaning up socket event listeners` ) ;
227+ debugLog ( `${ logPrefix } Cleaning up socket event listeners` ) ;
202228 socketRef . current . off ( "connect" , onConnect ) ;
203229 socketRef . current . off ( "disconnect" , onDisconnect ) ;
204230 socketRef . current . off ( "connect_error" , onConnectError ) ;
@@ -209,10 +235,10 @@ export function useMySocket({ deviceName, socketURL }: Props) {
209235 }
210236 } ;
211237 } catch ( error ) {
212- console . error ( `${ logPrefix } Failed to initialize socket:` , error ) ;
238+ debugError ( `${ logPrefix } Failed to initialize socket:` , error ) ;
213239 }
214240 // eslint-disable-next-line react-hooks/exhaustive-deps
215- } , [ persistentDeviceId ] ) ;
241+ } , [ persistentDeviceId , enableDebugLogs ] ) ;
216242
217243 // Update the socket query parameters when deviceName changes
218244 useEffect ( ( ) => {
@@ -221,15 +247,21 @@ export function useMySocket({ deviceName, socketURL }: Props) {
221247 socketRef . current . io . opts . query &&
222248 persistentDeviceId
223249 ) {
224- console . log ( `${ logPrefix } Updating device name in socket connection` ) ;
250+ debugLog ( `${ logPrefix } Updating device name in socket connection` ) ;
225251 socketRef . current . io . opts . query = {
226252 ...socketRef . current . io . opts . query ,
227253 deviceName,
228254 deviceId : persistentDeviceId ,
229255 platform : currentPlatform ,
230256 } ;
231257 }
232- } , [ deviceName , logPrefix , persistentDeviceId , currentPlatform ] ) ;
258+ } , [
259+ deviceName ,
260+ logPrefix ,
261+ persistentDeviceId ,
262+ currentPlatform ,
263+ enableDebugLogs ,
264+ ] ) ;
233265
234266 // Update the socket URL when socketURL changes
235267 useEffect ( ( ) => {
@@ -242,7 +274,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
242274 currentSocketURL !== platformUrl &&
243275 persistentDeviceId
244276 ) {
245- console . log (
277+ debugLog (
246278 `${ logPrefix } Socket URL changed from ${ currentSocketURL } to ${ platformUrl } `
247279 ) ;
248280
@@ -251,7 +283,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
251283 socketRef . current . disconnect ( ) ;
252284 currentSocketURL = platformUrl ;
253285
254- console . log (
286+ debugLog (
255287 `${ logPrefix } Creating new socket connection to ${ platformUrl } `
256288 ) ;
257289 globalSocketInstance = socketIO ( platformUrl , {
@@ -268,20 +300,24 @@ export function useMySocket({ deviceName, socketURL }: Props) {
268300 socketRef . current = globalSocketInstance ;
269301 setSocket ( socketRef . current ) ;
270302 } catch ( error ) {
271- console . error (
272- `${ logPrefix } Failed to update socket connection:` ,
273- error
274- ) ;
303+ debugError ( `${ logPrefix } Failed to update socket connection:` , error ) ;
275304 }
276305 }
277- } , [ socketURL , deviceName , logPrefix , persistentDeviceId , currentPlatform ] ) ;
306+ } , [
307+ socketURL ,
308+ deviceName ,
309+ logPrefix ,
310+ persistentDeviceId ,
311+ currentPlatform ,
312+ enableDebugLogs ,
313+ ] ) ;
278314
279315 /**
280316 * Manually connect to the socket server
281317 */
282318 function connect ( ) {
283319 if ( socketRef . current && ! socketRef . current . connected ) {
284- console . log ( `${ logPrefix } Manually connecting to socket server` ) ;
320+ debugLog ( `${ logPrefix } Manually connecting to socket server` ) ;
285321 socketRef . current . connect ( ) ;
286322 }
287323 }
@@ -291,7 +327,7 @@ export function useMySocket({ deviceName, socketURL }: Props) {
291327 */
292328 function disconnect ( ) {
293329 if ( socketRef . current && socketRef . current . connected ) {
294- console . log ( `${ logPrefix } Manually disconnecting from socket server` ) ;
330+ debugLog ( `${ logPrefix } Manually disconnecting from socket server` ) ;
295331 socketRef . current . disconnect ( ) ;
296332 }
297333 }
0 commit comments