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" ;
3
3
4
- import { log } from ' ./utils/logger' ;
5
- import { getPlatformSpecificURL , PlatformOS } from ' ./platformUtils' ;
4
+ import { log } from " ./utils/logger" ;
5
+ import { getPlatformSpecificURL , PlatformOS } from " ./platformUtils" ;
6
6
7
7
interface Props {
8
8
deviceName : string ; // Unique name to identify the device
@@ -16,14 +16,20 @@ interface Props {
16
16
* @default false
17
17
*/
18
18
enableLogs ?: boolean ;
19
+ /**
20
+ * Whether the app is running on a physical device or an emulator/simulator
21
+ * This can affect how the socket URL is constructed, especially on Android
22
+ * @default false
23
+ */
24
+ isDevice ?: boolean ; // Whether the app is running on a physical device
19
25
}
20
26
21
27
/**
22
28
* Create a singleton socket instance that persists across component renders
23
29
* This way multiple components can share the same socket connection
24
30
*/
25
31
let globalSocketInstance : Socket | null = null ;
26
- let currentSocketURL = '' ;
32
+ let currentSocketURL = "" ;
27
33
28
34
/**
29
35
* Hook that handles socket connection for device-dashboard communication
@@ -43,6 +49,7 @@ export function useMySocket({
43
49
envVariables,
44
50
platform,
45
51
enableLogs = false ,
52
+ isDevice = false ,
46
53
} : Props ) {
47
54
const socketRef = useRef < Socket | null > ( null ) ;
48
55
const [ socket , setSocket ] = useState < Socket | null > ( null ) ;
@@ -78,15 +85,19 @@ export function useMySocket({
78
85
} ;
79
86
80
87
const onConnectError = ( error : Error ) => {
81
- log ( `${ logPrefix } Socket connection error: ${ error . message } ` , enableLogs , 'error' ) ;
88
+ log (
89
+ `${ logPrefix } Socket connection error: ${ error . message } ` ,
90
+ enableLogs ,
91
+ "error"
92
+ ) ;
82
93
} ;
83
94
84
95
const onConnectTimeout = ( ) => {
85
- log ( `${ logPrefix } Socket connection timeout` , enableLogs , ' error' ) ;
96
+ log ( `${ logPrefix } Socket connection timeout` , enableLogs , " error" ) ;
86
97
} ;
87
98
88
99
// Get the platform-specific URL
89
- const platformUrl = getPlatformSpecificURL ( socketURL , platform ) ;
100
+ const platformUrl = getPlatformSpecificURL ( socketURL , platform , isDevice ) ;
90
101
currentSocketURL = platformUrl ;
91
102
92
103
try {
@@ -102,18 +113,21 @@ export function useMySocket({
102
113
envVariables : JSON . stringify ( envVariables ) ,
103
114
} ,
104
115
reconnection : false ,
105
- transports : [ ' websocket' ] , // Prefer websocket transport for React Native
116
+ transports : [ " websocket" ] , // Prefer websocket transport for React Native
106
117
} ) ;
107
118
} else {
108
- log ( `${ logPrefix } Reusing existing socket instance to ${ platformUrl } ` , enableLogs ) ;
119
+ log (
120
+ `${ logPrefix } Reusing existing socket instance to ${ platformUrl } ` ,
121
+ enableLogs
122
+ ) ;
109
123
}
110
124
111
125
socketRef . current = globalSocketInstance ;
112
126
setSocket ( socketRef . current ) ;
113
127
114
128
// Setup error event listener
115
- socketRef . current . on ( ' connect_error' , onConnectError ) ;
116
- socketRef . current . on ( ' connect_timeout' , onConnectTimeout ) ;
129
+ socketRef . current . on ( " connect_error" , onConnectError ) ;
130
+ socketRef . current . on ( " connect_timeout" , onConnectTimeout ) ;
117
131
118
132
// Check initial connection state
119
133
if ( socketRef . current . connected ) {
@@ -122,31 +136,39 @@ export function useMySocket({
122
136
}
123
137
124
138
// Set up event handlers
125
- socketRef . current . on ( ' connect' , onConnect ) ;
126
- socketRef . current . on ( ' disconnect' , onDisconnect ) ;
139
+ socketRef . current . on ( " connect" , onConnect ) ;
140
+ socketRef . current . on ( " disconnect" , onDisconnect ) ;
127
141
128
142
// Clean up event listeners on unmount but don't disconnect
129
143
return ( ) => {
130
144
if ( socketRef . current ) {
131
145
log ( `${ logPrefix } Cleaning up socket event listeners` , enableLogs ) ;
132
- socketRef . current . off ( ' connect' , onConnect ) ;
133
- socketRef . current . off ( ' disconnect' , onDisconnect ) ;
134
- socketRef . current . off ( ' connect_error' , onConnectError ) ;
135
- socketRef . current . off ( ' connect_timeout' , onConnectTimeout ) ;
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 ) ;
136
150
// Don't disconnect socket on component unmount
137
151
// We want it to remain connected for the app's lifetime
138
152
}
139
153
} ;
140
154
} catch ( error ) {
141
- log ( `${ logPrefix } Failed to initialize socket: ${ error } ` , enableLogs , 'error' ) ;
155
+ log (
156
+ `${ logPrefix } Failed to initialize socket: ${ error } ` ,
157
+ enableLogs ,
158
+ "error"
159
+ ) ;
142
160
}
143
161
// ## DON'T ADD ANYTHING ELSE TO THE DEPENDENCY ARRAY ###
144
162
// eslint-disable-next-line react-hooks/exhaustive-deps
145
163
} , [ persistentDeviceId ] ) ;
146
164
147
165
// Update the socket query parameters when deviceName changes
148
166
useEffect ( ( ) => {
149
- if ( socketRef . current && socketRef . current . io . opts . query && persistentDeviceId ) {
167
+ if (
168
+ socketRef . current &&
169
+ socketRef . current . io . opts . query &&
170
+ persistentDeviceId
171
+ ) {
150
172
socketRef . current . io . opts . query = {
151
173
...socketRef . current . io . opts . query ,
152
174
deviceName,
@@ -159,18 +181,28 @@ export function useMySocket({
159
181
// Update the socket URL when socketURL changes
160
182
useEffect ( ( ) => {
161
183
// Get platform-specific URL for the new socketURL
162
- const platformUrl = getPlatformSpecificURL ( socketURL , platform ) ;
184
+ const platformUrl = getPlatformSpecificURL ( socketURL , platform , isDevice ) ;
163
185
164
186
// Compare with last known URL to avoid direct property access
165
- if ( socketRef . current && currentSocketURL !== platformUrl && persistentDeviceId ) {
166
- log ( `${ logPrefix } Socket URL changed from ${ currentSocketURL } to ${ platformUrl } ` , enableLogs ) ;
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
+ ) ;
167
196
168
197
try {
169
198
// Only recreate socket if URL actually changed
170
199
socketRef . current . disconnect ( ) ;
171
200
currentSocketURL = platformUrl ;
172
201
173
- log ( `${ logPrefix } Creating new socket connection to ${ platformUrl } ` , enableLogs ) ;
202
+ log (
203
+ `${ logPrefix } Creating new socket connection to ${ platformUrl } ` ,
204
+ enableLogs
205
+ ) ;
174
206
globalSocketInstance = socketIO ( platformUrl , {
175
207
autoConnect : true ,
176
208
query : {
@@ -181,16 +213,29 @@ export function useMySocket({
181
213
envVariables : JSON . stringify ( envVariables ) ,
182
214
} ,
183
215
reconnection : false ,
184
- transports : [ ' websocket' ] , // Prefer websocket transport for React Native
216
+ transports : [ " websocket" ] , // Prefer websocket transport for React Native
185
217
} ) ;
186
218
187
219
socketRef . current = globalSocketInstance ;
188
220
setSocket ( socketRef . current ) ;
189
221
} catch ( error ) {
190
- log ( `${ logPrefix } Failed to update socket connection: ${ error } ` , enableLogs , 'error' ) ;
222
+ log (
223
+ `${ logPrefix } Failed to update socket connection: ${ error } ` ,
224
+ enableLogs ,
225
+ "error"
226
+ ) ;
191
227
}
192
228
}
193
- } , [ socketURL , deviceName , logPrefix , persistentDeviceId , platform , enableLogs , extraDeviceInfo , envVariables ] ) ;
229
+ } , [
230
+ socketURL ,
231
+ deviceName ,
232
+ logPrefix ,
233
+ persistentDeviceId ,
234
+ platform ,
235
+ enableLogs ,
236
+ extraDeviceInfo ,
237
+ envVariables ,
238
+ ] ) ;
194
239
195
240
/**
196
241
* Manually connect to the socket server
0 commit comments