File tree 3 files changed +24
-1
lines changed
3 files changed +24
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' livekit-client ' : patch
3
+ ---
4
+
5
+ Add experimental method to prepare connection for speeding up subsequent first connection attempt
Original file line number Diff line number Diff line change @@ -98,6 +98,11 @@ const appActions = {
98
98
) : Promise < Room | undefined > => {
99
99
const room = new Room ( roomOptions ) ;
100
100
101
+ startTime = Date . now ( ) ;
102
+ await room . prepareConnection ( url ) ;
103
+ const prewarmTime = Date . now ( ) - startTime ;
104
+ appendLog ( `prewarmed connection in ${ prewarmTime } ms` ) ;
105
+
101
106
room
102
107
. on ( RoomEvent . ParticipantConnected , participantConnected )
103
108
. on ( RoomEvent . ParticipantDisconnected , participantDisconnected )
@@ -149,6 +154,8 @@ const appActions = {
149
154
renderScreenShare ( room ) ;
150
155
} )
151
156
. on ( RoomEvent . SignalConnected , async ( ) => {
157
+ const signalConnectionTime = Date . now ( ) - startTime ;
158
+ appendLog ( `signal connection established in ${ signalConnectionTime } ms` ) ;
152
159
if ( shouldPublish ) {
153
160
await Promise . all ( [
154
161
room . localParticipant . setCameraEnabled ( true ) ,
@@ -159,7 +166,6 @@ const appActions = {
159
166
} ) ;
160
167
161
168
try {
162
- startTime = Date . now ( ) ;
163
169
await room . connect ( url , token , connectOptions ) ;
164
170
const elapsed = Date . now ( ) - startTime ;
165
171
appendLog (
Original file line number Diff line number Diff line change @@ -218,6 +218,18 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
218
218
return DeviceManager . getInstance ( ) . getDevices ( kind , requestPermissions ) ;
219
219
}
220
220
221
+ /**
222
+ * prepares the connection to the livekit server by sending a HEAD request in order to
223
+ * 1. speed up DNS resolution
224
+ * 2. speed up TLS setup
225
+ * on the actual connection request
226
+ * throws an error if server is not reachable after the request timeout
227
+ * @experimental
228
+ */
229
+ async prepareConnection ( url : string ) {
230
+ await fetch ( `http${ url . substring ( 2 ) } ` , { method : 'HEAD' } ) ;
231
+ }
232
+
221
233
connect = ( url : string , token : string , opts ?: RoomConnectOptions ) : Promise < void > => {
222
234
if ( this . state === ConnectionState . Connected ) {
223
235
// when the state is reconnecting or connected, this function returns immediately
You can’t perform that action at this time.
0 commit comments