1- import { useEffect , useRef , useState } from " react" ;
2- import { io as socketIO , Socket } from " socket.io-client" ;
1+ import { useEffect , useRef , useState } from ' react' ;
2+ import { io as socketIO , Socket } from ' socket.io-client' ;
33
4- import { log } from " ./utils/logger" ;
5- import { getPlatformSpecificURL , PlatformOS } from " ./platformUtils" ;
4+ import { log } from ' ./utils/logger' ;
5+ import { getPlatformSpecificURL , PlatformOS } from ' ./platformUtils' ;
66
77interface Props {
88 deviceName : string ; // Unique name to identify the device
@@ -16,20 +16,20 @@ interface Props {
1616 * @default false
1717 */
1818 enableLogs ?: boolean ;
19- /**
19+ /**
2020 * Whether the app is running on a physical device or an emulator/simulator
2121 * This can affect how the socket URL is constructed, especially on Android
2222 * @default false
2323 */
24- isDevice ?: boolean ; // Whether the app is running on a physical device
24+ isDevice ?: boolean // Whether the app is running on a physical device
2525}
2626
2727/**
2828 * Create a singleton socket instance that persists across component renders
2929 * This way multiple components can share the same socket connection
3030 */
3131let globalSocketInstance : Socket | null = null ;
32- let currentSocketURL = "" ;
32+ let currentSocketURL = '' ;
3333
3434/**
3535 * Hook that handles socket connection for device-dashboard communication
@@ -49,7 +49,7 @@ export function useMySocket({
4949 envVariables,
5050 platform,
5151 enableLogs = false ,
52- isDevice = false ,
52+ isDevice = false
5353} : Props ) {
5454 const socketRef = useRef < Socket | null > ( null ) ;
5555 const [ socket , setSocket ] = useState < Socket | null > ( null ) ;
@@ -85,15 +85,11 @@ export function useMySocket({
8585 } ;
8686
8787 const onConnectError = ( error : Error ) => {
88- log (
89- `${ logPrefix } Socket connection error: ${ error . message } ` ,
90- enableLogs ,
91- "error"
92- ) ;
88+ log ( `${ logPrefix } Socket connection error: ${ error . message } ` , enableLogs , 'error' ) ;
9389 } ;
9490
9591 const onConnectTimeout = ( ) => {
96- log ( `${ logPrefix } Socket connection timeout` , enableLogs , " error" ) ;
92+ log ( `${ logPrefix } Socket connection timeout` , enableLogs , ' error' ) ;
9793 } ;
9894
9995 // Get the platform-specific URL
@@ -113,21 +109,18 @@ export function useMySocket({
113109 envVariables : JSON . stringify ( envVariables ) ,
114110 } ,
115111 reconnection : false ,
116- transports : [ " websocket" ] , // Prefer websocket transport for React Native
112+ transports : [ ' websocket' ] , // Prefer websocket transport for React Native
117113 } ) ;
118114 } else {
119- log (
120- `${ logPrefix } Reusing existing socket instance to ${ platformUrl } ` ,
121- enableLogs
122- ) ;
115+ log ( `${ logPrefix } Reusing existing socket instance to ${ platformUrl } ` , enableLogs ) ;
123116 }
124117
125118 socketRef . current = globalSocketInstance ;
126119 setSocket ( socketRef . current ) ;
127120
128121 // Setup error event listener
129- socketRef . current . on ( " connect_error" , onConnectError ) ;
130- socketRef . current . on ( " connect_timeout" , onConnectTimeout ) ;
122+ socketRef . current . on ( ' connect_error' , onConnectError ) ;
123+ socketRef . current . on ( ' connect_timeout' , onConnectTimeout ) ;
131124
132125 // Check initial connection state
133126 if ( socketRef . current . connected ) {
@@ -136,39 +129,31 @@ export function useMySocket({
136129 }
137130
138131 // Set up event handlers
139- socketRef . current . on ( " connect" , onConnect ) ;
140- socketRef . current . on ( " disconnect" , onDisconnect ) ;
132+ socketRef . current . on ( ' connect' , onConnect ) ;
133+ socketRef . current . on ( ' disconnect' , onDisconnect ) ;
141134
142135 // Clean up event listeners on unmount but don't disconnect
143136 return ( ) => {
144137 if ( socketRef . current ) {
145138 log ( `${ logPrefix } Cleaning up socket event listeners` , enableLogs ) ;
146- socketRef . current . off ( " connect" , onConnect ) ;
147- socketRef . current . off ( " disconnect" , onDisconnect ) ;
148- socketRef . current . off ( " connect_error" , onConnectError ) ;
149- socketRef . current . off ( " connect_timeout" , onConnectTimeout ) ;
139+ socketRef . current . off ( ' connect' , onConnect ) ;
140+ socketRef . current . off ( ' disconnect' , onDisconnect ) ;
141+ socketRef . current . off ( ' connect_error' , onConnectError ) ;
142+ socketRef . current . off ( ' connect_timeout' , onConnectTimeout ) ;
150143 // Don't disconnect socket on component unmount
151144 // We want it to remain connected for the app's lifetime
152145 }
153146 } ;
154147 } catch ( error ) {
155- log (
156- `${ logPrefix } Failed to initialize socket: ${ error } ` ,
157- enableLogs ,
158- "error"
159- ) ;
148+ log ( `${ logPrefix } Failed to initialize socket: ${ error } ` , enableLogs , 'error' ) ;
160149 }
161150 // ## DON'T ADD ANYTHING ELSE TO THE DEPENDENCY ARRAY ###
162151 // eslint-disable-next-line react-hooks/exhaustive-deps
163152 } , [ persistentDeviceId ] ) ;
164153
165154 // Update the socket query parameters when deviceName changes
166155 useEffect ( ( ) => {
167- if (
168- socketRef . current &&
169- socketRef . current . io . opts . query &&
170- persistentDeviceId
171- ) {
156+ if ( socketRef . current && socketRef . current . io . opts . query && persistentDeviceId ) {
172157 socketRef . current . io . opts . query = {
173158 ...socketRef . current . io . opts . query ,
174159 deviceName,
@@ -184,25 +169,15 @@ export function useMySocket({
184169 const platformUrl = getPlatformSpecificURL ( socketURL , platform , isDevice ) ;
185170
186171 // Compare with last known URL to avoid direct property access
187- if (
188- socketRef . current &&
189- currentSocketURL !== platformUrl &&
190- persistentDeviceId
191- ) {
192- log (
193- `${ logPrefix } Socket URL changed from ${ currentSocketURL } to ${ platformUrl } ` ,
194- enableLogs
195- ) ;
172+ if ( socketRef . current && currentSocketURL !== platformUrl && persistentDeviceId ) {
173+ log ( `${ logPrefix } Socket URL changed from ${ currentSocketURL } to ${ platformUrl } ` , enableLogs ) ;
196174
197175 try {
198176 // Only recreate socket if URL actually changed
199177 socketRef . current . disconnect ( ) ;
200178 currentSocketURL = platformUrl ;
201179
202- log (
203- `${ logPrefix } Creating new socket connection to ${ platformUrl } ` ,
204- enableLogs
205- ) ;
180+ log ( `${ logPrefix } Creating new socket connection to ${ platformUrl } ` , enableLogs ) ;
206181 globalSocketInstance = socketIO ( platformUrl , {
207182 autoConnect : true ,
208183 query : {
@@ -213,29 +188,16 @@ export function useMySocket({
213188 envVariables : JSON . stringify ( envVariables ) ,
214189 } ,
215190 reconnection : false ,
216- transports : [ " websocket" ] , // Prefer websocket transport for React Native
191+ transports : [ ' websocket' ] , // Prefer websocket transport for React Native
217192 } ) ;
218193
219194 socketRef . current = globalSocketInstance ;
220195 setSocket ( socketRef . current ) ;
221196 } catch ( error ) {
222- log (
223- `${ logPrefix } Failed to update socket connection: ${ error } ` ,
224- enableLogs ,
225- "error"
226- ) ;
197+ log ( `${ logPrefix } Failed to update socket connection: ${ error } ` , enableLogs , 'error' ) ;
227198 }
228199 }
229- } , [
230- socketURL ,
231- deviceName ,
232- logPrefix ,
233- persistentDeviceId ,
234- platform ,
235- enableLogs ,
236- extraDeviceInfo ,
237- envVariables ,
238- ] ) ;
200+ } , [ socketURL , deviceName , logPrefix , persistentDeviceId , platform , enableLogs , extraDeviceInfo , envVariables ] ) ;
239201
240202 /**
241203 * Manually connect to the socket server
0 commit comments