@phystack/hub-client 4.5.19-dev → 4.5.20-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/dist/index.d.ts +22 -28
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +252 -378
  4. package/dist/index.js.map +1 -1
  5. package/dist/peripheral-twin.d.ts +34 -0
  6. package/dist/peripheral-twin.d.ts.map +1 -0
  7. package/dist/peripheral-twin.js +234 -0
  8. package/dist/peripheral-twin.js.map +1 -0
  9. package/dist/services/phyhub-connection.service.d.ts +1 -0
  10. package/dist/services/phyhub-connection.service.d.ts.map +1 -1
  11. package/dist/services/phyhub-connection.service.js +17 -0
  12. package/dist/services/phyhub-connection.service.js.map +1 -1
  13. package/dist/services/phyhub-direct-connection.service.d.ts +21 -0
  14. package/dist/services/phyhub-direct-connection.service.d.ts.map +1 -0
  15. package/dist/services/phyhub-direct-connection.service.js +101 -0
  16. package/dist/services/phyhub-direct-connection.service.js.map +1 -0
  17. package/dist/services/webrtc/data-channel-handler.d.ts +45 -0
  18. package/dist/services/webrtc/data-channel-handler.d.ts.map +1 -0
  19. package/dist/services/webrtc/data-channel-handler.js +260 -0
  20. package/dist/services/webrtc/data-channel-handler.js.map +1 -0
  21. package/dist/services/webrtc/index.d.ts +8 -0
  22. package/dist/services/webrtc/index.d.ts.map +1 -0
  23. package/dist/services/webrtc/index.js +18 -0
  24. package/dist/services/webrtc/index.js.map +1 -0
  25. package/dist/services/webrtc/media-stream-handler.d.ts +57 -0
  26. package/dist/services/webrtc/media-stream-handler.d.ts.map +1 -0
  27. package/dist/services/webrtc/media-stream-handler.js +383 -0
  28. package/dist/services/webrtc/media-stream-handler.js.map +1 -0
  29. package/dist/services/webrtc/peer-connection-manager.d.ts +40 -0
  30. package/dist/services/webrtc/peer-connection-manager.d.ts.map +1 -0
  31. package/dist/services/webrtc/peer-connection-manager.js +336 -0
  32. package/dist/services/webrtc/peer-connection-manager.js.map +1 -0
  33. package/dist/services/webrtc/types.d.ts +134 -0
  34. package/dist/services/webrtc/types.d.ts.map +1 -0
  35. package/dist/services/webrtc/types.js +12 -0
  36. package/dist/services/webrtc/types.js.map +1 -0
  37. package/dist/services/webrtc/webrtc-globals.d.ts +4 -0
  38. package/dist/services/webrtc/webrtc-globals.d.ts.map +1 -0
  39. package/dist/services/webrtc/webrtc-globals.js +72 -0
  40. package/dist/services/webrtc/webrtc-globals.js.map +1 -0
  41. package/dist/services/webrtc/webrtc-manager.d.ts +35 -0
  42. package/dist/services/webrtc/webrtc-manager.d.ts.map +1 -0
  43. package/dist/services/webrtc/webrtc-manager.js +274 -0
  44. package/dist/services/webrtc/webrtc-manager.js.map +1 -0
  45. package/dist/test/communication-comprehensive-test.d.ts +8 -0
  46. package/dist/test/communication-comprehensive-test.d.ts.map +1 -0
  47. package/dist/test/communication-comprehensive-test.js +356 -0
  48. package/dist/test/communication-comprehensive-test.js.map +1 -0
  49. package/dist/test/webrtc-channel-names-test.d.ts +2 -0
  50. package/dist/test/webrtc-channel-names-test.d.ts.map +1 -0
  51. package/dist/test/webrtc-channel-names-test.js +177 -0
  52. package/dist/test/webrtc-channel-names-test.js.map +1 -0
  53. package/dist/test/webrtc-comprehensive-test.d.ts +2 -0
  54. package/dist/test/webrtc-comprehensive-test.d.ts.map +1 -0
  55. package/dist/test/webrtc-comprehensive-test.js +328 -0
  56. package/dist/test/webrtc-comprehensive-test.js.map +1 -0
  57. package/dist/test/webrtc-reconnect-test.d.ts +4 -0
  58. package/dist/test/webrtc-reconnect-test.d.ts.map +1 -0
  59. package/dist/test/webrtc-reconnect-test.js +244 -0
  60. package/dist/test/webrtc-reconnect-test.js.map +1 -0
  61. package/dist/test/webrtc-test-harness.d.ts +4 -0
  62. package/dist/test/webrtc-test-harness.d.ts.map +1 -0
  63. package/dist/test/webrtc-test-harness.js +169 -0
  64. package/dist/test/webrtc-test-harness.js.map +1 -0
  65. package/dist/twin-messaging.d.ts +20 -0
  66. package/dist/twin-messaging.d.ts.map +1 -0
  67. package/dist/twin-messaging.js +94 -0
  68. package/dist/twin-messaging.js.map +1 -0
  69. package/dist/twin-registry.d.ts +9 -0
  70. package/dist/twin-registry.d.ts.map +1 -0
  71. package/dist/twin-registry.js +26 -0
  72. package/dist/twin-registry.js.map +1 -0
  73. package/dist/types/index.d.ts +4 -0
  74. package/dist/types/index.d.ts.map +1 -0
  75. package/dist/types/index.js +20 -0
  76. package/dist/types/index.js.map +1 -0
  77. package/dist/types/twin.types.d.ts +62 -14
  78. package/dist/types/twin.types.d.ts.map +1 -1
  79. package/dist/types/twin.types.js +8 -1
  80. package/dist/types/twin.types.js.map +1 -1
  81. package/docs/webrtc-howto.md +398 -0
  82. package/docs/webrtc-test.md +330 -0
  83. package/package.json +3 -3
  84. package/scripts/webrtc-test.sh +401 -0
  85. package/src/index.ts +378 -568
  86. package/src/peripheral-twin.ts +337 -0
  87. package/src/services/phyhub-connection.service.ts +24 -0
  88. package/src/services/phyhub-direct-connection.service.ts +159 -0
  89. package/src/services/webrtc/data-channel-handler.ts +362 -0
  90. package/src/services/webrtc/index.ts +36 -0
  91. package/src/services/webrtc/media-stream-handler.ts +536 -0
  92. package/src/services/webrtc/peer-connection-manager.ts +467 -0
  93. package/src/services/webrtc/types.ts +273 -0
  94. package/src/services/webrtc/webrtc-globals.ts +108 -0
  95. package/src/services/webrtc/webrtc-manager.ts +490 -0
  96. package/src/test/communication-comprehensive-test.ts +533 -0
  97. package/src/test/webrtc-channel-names-test.ts +266 -0
  98. package/src/test/webrtc-comprehensive-test.ts +494 -0
  99. package/src/test/webrtc-reconnect-test.ts +345 -0
  100. package/src/test/webrtc-test-harness.ts +254 -0
  101. package/src/twin-messaging.ts +184 -0
  102. package/src/twin-registry.ts +39 -0
  103. package/src/types/index.ts +3 -0
  104. package/src/types/twin.types.ts +80 -14
  105. package/dist/services/webrtc/datachannel.d.ts +0 -10
  106. package/dist/services/webrtc/datachannel.d.ts.map +0 -1
  107. package/dist/services/webrtc/datachannel.js +0 -290
  108. package/dist/services/webrtc/datachannel.js.map +0 -1
  109. package/dist/services/webrtc/mediastream.d.ts +0 -10
  110. package/dist/services/webrtc/mediastream.d.ts.map +0 -1
  111. package/dist/services/webrtc/mediastream.js +0 -396
  112. package/dist/services/webrtc/mediastream.js.map +0 -1
  113. package/dist/services/webrtc/peer-connection-ice.d.ts +0 -32
  114. package/dist/services/webrtc/peer-connection-ice.d.ts.map +0 -1
  115. package/dist/services/webrtc/peer-connection-ice.js +0 -483
  116. package/dist/services/webrtc/peer-connection-ice.js.map +0 -1
  117. package/src/services/webrtc/datachannel.ts +0 -421
  118. package/src/services/webrtc/mediastream.ts +0 -602
  119. package/src/services/webrtc/peer-connection-ice.ts +0 -689
@@ -0,0 +1,330 @@
1
+ # WebRTC Testing Guide
2
+
3
+ This guide explains how to test the WebRTC functionality in hub-client using direct PhyHub connections.
4
+
5
+ ## Overview
6
+
7
+ The hub-client WebRTC system enables peer-to-peer DataChannel and MediaStream connections between twins. Testing requires two connected endpoints, which we achieve by running two processes with different device credentials.
8
+
9
+ ## Architecture
10
+
11
+ ```
12
+ Normal Operation:
13
+ App (hub-client) → phyos (on device) → PhyHub (cloud)
14
+ {instanceId} {deviceId, accessKey}
15
+
16
+ Testing Mode (PHYHUB_DIRECT=true):
17
+ Test Script → PhyHub (cloud)
18
+ {deviceId, accessKey}
19
+ ```
20
+
21
+ ## Prerequisites
22
+
23
+ 1. **Two device credentials** - You need credentials for two separate devices
24
+ 2. **PhyHub access** - Devices must be registered and accessible
25
+ 3. **Node.js & ts-node** - For running the test harness
26
+
27
+ ## Getting Device Credentials
28
+
29
+ Use the Phygrid CLI to get device credentials:
30
+
31
+ ```bash
32
+ # List available devices
33
+ phy dev list
34
+
35
+ # Select a device interactively
36
+ phy dev select
37
+
38
+ # Export credentials as environment variables
39
+ phy dev develop --export
40
+ ```
41
+
42
+ This will output something like:
43
+ ```bash
44
+ export PHYGRID_DEVICE_ID=abc123...
45
+ export PHYGRID_DEVICE_KEY=xyz789...
46
+ ```
47
+
48
+ Repeat for a second device to get two sets of credentials.
49
+
50
+ ## Running the Test Harness
51
+
52
+ The test requires two terminals - one for each endpoint.
53
+
54
+ ### Terminal 1: Responder (Start First)
55
+
56
+ ```bash
57
+ cd packages/hub-client
58
+
59
+ PHYHUB_DIRECT=true \
60
+ DEVICE_ID=<device-b-id> \
61
+ ACCESS_KEY=<device-b-key> \
62
+ PEER_TWIN_ID=<device-a-id> \
63
+ ROLE=responder \
64
+ npx ts-node src/test/webrtc-test-harness.ts
65
+ ```
66
+
67
+ ### Terminal 2: Initiator
68
+
69
+ ```bash
70
+ cd packages/hub-client
71
+
72
+ PHYHUB_DIRECT=true \
73
+ DEVICE_ID=<device-a-id> \
74
+ ACCESS_KEY=<device-a-key> \
75
+ PEER_TWIN_ID=<device-b-id> \
76
+ ROLE=initiator \
77
+ npx ts-node src/test/webrtc-test-harness.ts
78
+ ```
79
+
80
+ ## Environment Variables
81
+
82
+ | Variable | Required | Description |
83
+ |----------|----------|-------------|
84
+ | `PHYHUB_DIRECT` | Yes | Must be `true` to enable direct connection |
85
+ | `DEVICE_ID` | Yes | Your device ID (also used as twin ID) |
86
+ | `ACCESS_KEY` | Yes | Your device access key |
87
+ | `PEER_TWIN_ID` | Yes | Twin ID of the peer to connect to |
88
+ | `ROLE` | Yes | `initiator` or `responder` |
89
+ | `PHYHUB_URL` | No | PhyHub URL (default: EU region) |
90
+ | `PHYHUB_REGION` | No | `eu`, `us`, or `local` |
91
+
92
+ Alternative variable names (for CLI compatibility):
93
+ - `PHYGRID_DEVICE_ID` instead of `DEVICE_ID`
94
+ - `PHYGRID_DEVICE_KEY` instead of `ACCESS_KEY`
95
+
96
+ ## Test Flow
97
+
98
+ 1. **Responder** connects to PhyHub and waits for incoming DataChannel
99
+ 2. **Initiator** connects to PhyHub and creates DataChannel to responder
100
+ 3. **WebRTC signaling** happens via twin messaging (offer/answer/ICE)
101
+ 4. **Initiator** sends 5 test messages with timestamps
102
+ 5. **Responder** echoes each message back
103
+ 6. **Both sides** log all events and messages
104
+
105
+ ## Expected Output
106
+
107
+ ### Responder Output
108
+ ```
109
+ ============================================================
110
+ WebRTC Test Harness
111
+ ============================================================
112
+
113
+ Configuration:
114
+ Device ID: device-b-id
115
+ Peer Twin ID: device-a-id
116
+ Role: responder
117
+ PhyHub URL: (default EU)
118
+
119
+ === Running as RESPONDER ===
120
+
121
+ Connecting to PhyHub...
122
+ Connected!
123
+
124
+ Waiting for DataChannel from peer: device-a-id
125
+ Ready to accept connection...
126
+
127
+ [EVENT] Connected to device-a-id (datachannel)
128
+ DataChannel received!
129
+
130
+ [RECEIVED] { type: 'test', count: 1, timestamp: 1234567890 }
131
+ [SENDING] { type: 'echo', original: {...}, respondedAt: 1234567891 }
132
+ ...
133
+
134
+ Channel closed. Received 5 messages.
135
+
136
+ [SUCCESS] Test completed successfully!
137
+ ```
138
+
139
+ ### Initiator Output
140
+ ```
141
+ ============================================================
142
+ WebRTC Test Harness
143
+ ============================================================
144
+
145
+ Configuration:
146
+ Device ID: device-a-id
147
+ Peer Twin ID: device-b-id
148
+ Role: initiator
149
+ PhyHub URL: (default EU)
150
+
151
+ === Running as INITIATOR ===
152
+
153
+ Connecting to PhyHub...
154
+ Connected!
155
+
156
+ Creating DataChannel to peer: device-b-id
157
+ Waiting for responder to be ready...
158
+
159
+ [EVENT] Connected to device-b-id (datachannel)
160
+ DataChannel created successfully!
161
+
162
+ Sending test messages...
163
+
164
+ [SENDING] { type: 'test', count: 1, timestamp: 1234567890 }
165
+ [RECEIVED] { type: 'echo', original: {...}, respondedAt: 1234567891 }
166
+ ...
167
+
168
+ Test complete. Closing channel...
169
+
170
+ [SUCCESS] Test completed successfully!
171
+ ```
172
+
173
+ ## Testing Against Local PhyHub
174
+
175
+ For local development, you can test against a local PhyHub instance:
176
+
177
+ ```bash
178
+ PHYHUB_DIRECT=true \
179
+ PHYHUB_URL=http://localhost:3000 \
180
+ DEVICE_ID=<device-id> \
181
+ ACCESS_KEY=<access-key> \
182
+ PEER_TWIN_ID=<peer-id> \
183
+ ROLE=initiator \
184
+ npx ts-node src/test/webrtc-test-harness.ts
185
+ ```
186
+
187
+ ## Troubleshooting
188
+
189
+ ### Connection Timeout
190
+ - Ensure both devices are registered in PhyHub
191
+ - Check that device credentials are correct
192
+ - Verify PhyHub is accessible (try EU vs US region)
193
+
194
+ ### No DataChannel Received
195
+ - Start the **responder first**, then the initiator
196
+ - Ensure `PEER_TWIN_ID` values are swapped correctly between terminals
197
+ - Check that twin messaging is working (devices subscribed to each other)
198
+
199
+ ### ICE Connection Failed
200
+ - Both peers need to be able to reach STUN servers
201
+ - Try with/without STUN: the system auto-toggles on first failure
202
+ - Check firewall settings
203
+
204
+ ### Authentication Failed
205
+ - Verify `ACCESS_KEY` matches the `DEVICE_ID`
206
+ - Ensure the device exists in PhyHub (was migrated from legacy)
207
+
208
+ ## Programmatic Testing
209
+
210
+ You can also use the test functions programmatically:
211
+
212
+ ```typescript
213
+ import { runInitiator, runResponder } from './test/webrtc-test-harness';
214
+
215
+ // In your test file
216
+ describe('WebRTC', () => {
217
+ it('should establish DataChannel connection', async () => {
218
+ // Run in separate processes or use test fixtures
219
+ });
220
+ });
221
+ ```
222
+
223
+ ## WebRTC Manager API
224
+
225
+ For more control during testing, use the WebRTCManager directly:
226
+
227
+ ```typescript
228
+ const client = await PhyHubClient.connect();
229
+ const manager = await client.getWebRTCManager({ verbose: true });
230
+
231
+ // Listen for all events
232
+ manager.on('connected', (data) => console.log('Connected:', data));
233
+ manager.on('disconnected', (data) => console.log('Disconnected:', data));
234
+ manager.on('error', (data) => console.log('Error:', data));
235
+ manager.on('reconnecting', (data) => console.log('Reconnecting:', data));
236
+ manager.on('reconnected', (data) => console.log('Reconnected:', data));
237
+
238
+ // Create connections
239
+ const channel = await manager.createDataChannel(targetTwinId);
240
+ const stream = await manager.createMediaStream(targetTwinId);
241
+ ```
242
+
243
+ ## Automated Two-Host Testing
244
+
245
+ For realistic testing across network boundaries, use the `webrtc-test.sh` script which:
246
+ - Stores credentials for two devices in `.env.webrtc-test`
247
+ - Syncs code to a remote Linux host via rsync
248
+ - Runs responder on remote host via SSH
249
+ - Runs initiator locally
250
+
251
+ ### Setup
252
+
253
+ 1. Create `.env.webrtc-test` with credentials for both devices:
254
+
255
+ ```bash
256
+ # Run the setup wizard
257
+ ./scripts/webrtc-test.sh setup
258
+ ```
259
+
260
+ Or manually create `.env.webrtc-test`:
261
+
262
+ ```bash
263
+ # Device A (local initiator)
264
+ DEVICE_A_ID=your-device-a-id
265
+ DEVICE_A_KEY=your-device-a-access-key
266
+
267
+ # Device B (remote responder)
268
+ DEVICE_B_ID=your-device-b-id
269
+ DEVICE_B_KEY=your-device-b-access-key
270
+
271
+ # Remote host configuration
272
+ REMOTE_HOST=user@remote-linux-host
273
+ REMOTE_PATH=/tmp/hub-client-test
274
+
275
+ # Optional
276
+ PHYHUB_REGION=eu
277
+ ```
278
+
279
+ 2. Run the test:
280
+
281
+ ```bash
282
+ ./scripts/webrtc-test.sh run
283
+ ```
284
+
285
+ ### What the Script Does
286
+
287
+ 1. **Syncs** the hub-client package to the remote host via rsync
288
+ 2. **Installs** dependencies on remote if needed
289
+ 3. **Starts** responder on remote host via SSH (backgrounded)
290
+ 4. **Waits** for responder to be ready
291
+ 5. **Runs** initiator locally
292
+ 6. **Collects** logs from both sides
293
+ 7. **Cleans up** remote processes
294
+
295
+ ### Manual Two-Host Testing
296
+
297
+ If you prefer manual control:
298
+
299
+ ```bash
300
+ # On remote host (responder)
301
+ ssh user@remote-host
302
+ cd /path/to/hub-client
303
+ PHYHUB_DIRECT=true \
304
+ DEVICE_ID=$DEVICE_B_ID \
305
+ ACCESS_KEY=$DEVICE_B_KEY \
306
+ PEER_TWIN_ID=$DEVICE_A_ID \
307
+ ROLE=responder \
308
+ npx ts-node src/test/webrtc-test-harness.ts
309
+
310
+ # On local host (initiator)
311
+ PHYHUB_DIRECT=true \
312
+ DEVICE_ID=$DEVICE_A_ID \
313
+ ACCESS_KEY=$DEVICE_A_KEY \
314
+ PEER_TWIN_ID=$DEVICE_B_ID \
315
+ ROLE=initiator \
316
+ npx ts-node src/test/webrtc-test-harness.ts
317
+ ```
318
+
319
+ ## Files Reference
320
+
321
+ | File | Purpose |
322
+ |------|---------|
323
+ | `src/test/webrtc-test-harness.ts` | Main test script |
324
+ | `scripts/webrtc-test.sh` | Automated two-host test runner |
325
+ | `.env.webrtc-test` | Test credentials (gitignored) |
326
+ | `src/services/phyhub-direct-connection.service.ts` | Direct PhyHub connection |
327
+ | `src/services/webrtc/WebRTCManager.ts` | WebRTC manager class |
328
+ | `src/services/webrtc/DataChannelHandler.ts` | DataChannel implementation |
329
+ | `src/services/webrtc/MediaStreamHandler.ts` | MediaStream implementation |
330
+ | `src/services/webrtc/PeerConnectionManager.ts` | ICE/connection management |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phystack/hub-client",
3
- "version": "4.5.19-dev",
3
+ "version": "4.5.20-dev",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -14,7 +14,7 @@
14
14
  "format:check": "prettier --check \"src/**/*.{ts,js,json,md}\""
15
15
  },
16
16
  "dependencies": {
17
- "@phystack/socket.io-proxy": "4.5.19-dev",
17
+ "@phystack/socket.io-proxy": "4.5.20-dev",
18
18
  "@roamhq/wrtc": "^0.8.0",
19
19
  "axios": "^1.7.9",
20
20
  "bufferutil": "^4.0.9",
@@ -46,5 +46,5 @@
46
46
  },
47
47
  "test": "react-scripts test",
48
48
  "test:coverage": "npm run test -- --coverage --watchAll=false",
49
- "gitHead": "2a35e91d1c5691e5e3d33449dfab2737b5215b22"
49
+ "gitHead": "2c6f557c48d6bfa40754a5f59e22c40e7cc5d29c"
50
50
  }