Skip to content

Commit

Permalink
Add experimental method to prepare connection in order to speed up su…
Browse files Browse the repository at this point in the history
…bsequent ws connection (livekit#469)

* add method to prepare connection for speeding up subsequent first connection attempt

* changeset

* update comment

* mark as experimental

* update changeset
  • Loading branch information
lukasIO committed Oct 13, 2022
1 parent 951a07c commit fcad243
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/smooth-dots-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Add experimental method to prepare connection for speeding up subsequent first connection attempt
8 changes: 7 additions & 1 deletion example/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ const appActions = {
): Promise<Room | undefined> => {
const room = new Room(roomOptions);

startTime = Date.now();
await room.prepareConnection(url);
const prewarmTime = Date.now() - startTime;
appendLog(`prewarmed connection in ${prewarmTime}ms`);

room
.on(RoomEvent.ParticipantConnected, participantConnected)
.on(RoomEvent.ParticipantDisconnected, participantDisconnected)
Expand Down Expand Up @@ -149,6 +154,8 @@ const appActions = {
renderScreenShare(room);
})
.on(RoomEvent.SignalConnected, async () => {
const signalConnectionTime = Date.now() - startTime;
appendLog(`signal connection established in ${signalConnectionTime}ms`);
if (shouldPublish) {
await Promise.all([
room.localParticipant.setCameraEnabled(true),
Expand All @@ -159,7 +166,6 @@ const appActions = {
});

try {
startTime = Date.now();
await room.connect(url, token, connectOptions);
const elapsed = Date.now() - startTime;
appendLog(
Expand Down
12 changes: 12 additions & 0 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
return DeviceManager.getInstance().getDevices(kind, requestPermissions);
}

/**
* prepares the connection to the livekit server by sending a HEAD request in order to
* 1. speed up DNS resolution
* 2. speed up TLS setup
* on the actual connection request
* throws an error if server is not reachable after the request timeout
* @experimental
*/
async prepareConnection(url: string) {
await fetch(`http${url.substring(2)}`, { method: 'HEAD' });
}

connect = (url: string, token: string, opts?: RoomConnectOptions): Promise<void> => {
if (this.state === ConnectionState.Connected) {
// when the state is reconnecting or connected, this function returns immediately
Expand Down

0 comments on commit fcad243

Please sign in to comment.