-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: Sent webhook when room is finished
- Loading branch information
Showing
7 changed files
with
82 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import express, { Router } from 'express'; | ||
import { webhookHandler } from '../controllers/webhook.controller.js'; | ||
import { lkWebhookHandler } from '../controllers/livekit-webhook.controller.js'; | ||
|
||
const livekitRouter = Router(); | ||
|
||
livekitRouter.use(express.raw({ type: 'application/webhook+json' })); | ||
livekitRouter.post('/webhook', webhookHandler); | ||
livekitRouter.post('/webhook', lkWebhookHandler); | ||
|
||
export { livekitRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Room } from 'livekit-server-sdk'; | ||
import { LoggerService } from './logger.service.js'; | ||
import { CALL_WEBHOOK_URL } from '../config.js'; | ||
|
||
export class OpenViduWebhookService { | ||
private static instance: OpenViduWebhookService; | ||
protected logger = LoggerService.getInstance(); | ||
|
||
static getInstance(): OpenViduWebhookService { | ||
if (!OpenViduWebhookService.instance) { | ||
OpenViduWebhookService.instance = new OpenViduWebhookService(); | ||
} | ||
|
||
return OpenViduWebhookService.instance; | ||
} | ||
|
||
async sendRoomFinishedWebhook(room: Room): Promise<void> { | ||
this.logger.verbose(`Sending room finished webhook for room ${room.name}`); | ||
|
||
try { | ||
await this.fetchWithRetry(CALL_WEBHOOK_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
event: 'room_finished', | ||
room: { | ||
name: room.name | ||
} | ||
}) | ||
}); | ||
} catch (error) { | ||
this.logger.error(`Error sending room finished webhook: ${error}`); | ||
} | ||
} | ||
|
||
private async fetchWithRetry(url: string, options: RequestInit, retries = 5, delay = 300): Promise<void> { | ||
try { | ||
// Try to fetch the URL | ||
const response = await fetch(url, options); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Request failed with status ${response.status}`); | ||
} | ||
} catch (error) { | ||
if (retries <= 0) { | ||
throw new Error(`Request failed: ${error}`); | ||
} | ||
|
||
this.logger.verbose(`Retrying in ${delay / 1000} seconds... (${retries} retries left)`); | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
// Retry the request after a delay with exponential backoff | ||
return this.fetchWithRetry(url, options, retries - 1, delay * 2); | ||
} | ||
} | ||
} |