Skip to content

Commit

Permalink
[mirotalksfu] - add webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Jan 15, 2025
1 parent 534f2f7 commit 57faf15
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 7 deletions.
47 changes: 44 additions & 3 deletions app/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dev dependencies: {
* @license For commercial or closed source, contact us at license.mirotalk@gmail.com or purchase directly via CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970
* @author Miroslav Pejic - miroslav.pejic.85@gmail.com
* @version 1.7.02
* @version 1.7.03
*
*/

Expand Down Expand Up @@ -181,6 +181,12 @@ if (sentryEnabled) {
*/
}

// Handle WebHook
const webhook = {
enabled: config?.webhook?.enabled || false,
url: config?.webhook?.url || 'http://localhost:8888/webhook-endpoint',
};

// Discord Bot
const { enabled, commands, token } = config.discord || {};

Expand Down Expand Up @@ -1182,6 +1188,7 @@ function startServer() {
stats_enabled: config.stats?.enabled ? config.stats : false,
ngrok_enabled: config.ngrok?.enabled ? config.ngrok : false,
email_alerts: config.email?.alert ? config.email : false,
webhook: webhook,

// Version Information
app_version: packageJson.version,
Expand Down Expand Up @@ -1526,6 +1533,15 @@ function startServer() {
}); // config.email.alert: true
}

// handle WebHook
if (webhook.enabled) {
// Trigger a POST request when a user joins
axios
.post(webhook.url, { event: 'join', data })
.then((response) => log.debug('Join event tracked:', response.data))
.catch((error) => log.error('Error tracking join event:', error.message));
}

cb(room.toJson());
});

Expand Down Expand Up @@ -2789,7 +2805,7 @@ function startServer() {
room.broadCast(socket.id, 'editorUpdate', data);
});

socket.on('disconnect', () => {
socket.on('disconnect', (reason) => {
if (!roomExists(socket)) return;

const { room, peer } = getRoomAndPeer(socket);
Expand All @@ -2798,7 +2814,7 @@ function startServer() {

const isPresenter = isPeerPresenter(socket.room_id, socket.id, peer_name, peer_uuid);

log.debug('[Disconnect] - peer name', peer_name);
log.debug('[Disconnect] - peer name', { peer_name, reason });

room.removePeer(socket.id);

Expand All @@ -2825,6 +2841,19 @@ function startServer() {

if (isPresenter) removeIP(socket);

if (webhook.enabled) {
const data = {
peer_name: peer_name,
presenter: isPresenter,
reason: reason,
};
// Trigger a POST request when a user disconnects
axios
.post(webhook.url, { event: 'disconnect', data })
.then((response) => log.debug('Disconnect event tracked:', response.data))
.catch((error) => log.error('Error tracking disconnect event:', error.message));
}

socket.room_id = null;
});

Expand Down Expand Up @@ -2870,6 +2899,18 @@ function startServer() {

if (isPresenter) removeIP(socket);

if (webhook.enabled) {
const data = {
peer_name: peer_name,
presenter: isPresenter,
};
// Trigger a POST request when a user exits
axios
.post(webhook.url, { event: 'exit', data })
.then((response) => log.debug('ExitROom event tracked:', response.data))
.catch((error) => log.error('Error tracking exitRoom event:', error.message));
}

callback('Successfully exited room');
});

Expand Down
8 changes: 8 additions & 0 deletions app/src/config.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ module.exports = {
DSN: '',
tracesSampleRate: 0.5,
},
webhook: {
/*
Enable or disable webhook functionality.
Set `enabled` to `true` to activate webhook sending of socket events (join, exitRoom, disconnect)
*/
enabled: false,
url: 'https://your-site.com/webhook-endpoint',
},
mattermost: {
/*
Mattermost: https://mattermost.com
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mirotalksfu",
"version": "1.7.02",
"version": "1.7.03",
"description": "WebRTC SFU browser-based video calls",
"main": "Server.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions public/js/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (location.href.substr(0, 5) !== 'https') location.href = 'https' + location.h
* @license For commercial or closed source, contact us at license.mirotalk@gmail.com or purchase directly via CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970
* @author Miroslav Pejic - miroslav.pejic.85@gmail.com
* @version 1.7.02
* @version 1.7.03
*
*/

Expand Down Expand Up @@ -4904,7 +4904,7 @@ function showAbout() {
imageUrl: image.about,
customClass: { image: 'img-about' },
position: 'center',
title: 'WebRTC SFU v1.7.02',
title: 'WebRTC SFU v1.7.03',
html: `
<br />
<div id="about">
Expand Down
2 changes: 1 addition & 1 deletion public/js/RoomClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license For commercial or closed source, contact us at license.mirotalk@gmail.com or purchase directly via CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970
* @author Miroslav Pejic - miroslav.pejic.85@gmail.com
* @version 1.7.02
* @version 1.7.03
*
*/

Expand Down
19 changes: 19 additions & 0 deletions webhook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "mirotalk-webhook",
"version": "1.0.0",
"description": "MiroTalk WebHook endpoint example",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"keywords": [
"webhook",
"server"
],
"author": "Miroslav Pejic",
"license": "AGPLv3",
"dependencies": {
"express": "4.21.2",
"body-parser": "1.20.3"
}
}
41 changes: 41 additions & 0 deletions webhook/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const port = 8888;

// Middleware to parse JSON bodies
app.use(bodyParser.json());

// Endpoint to receive webhooks
app.post('/webhook-endpoint', (req, res) => {
const { event, data } = req.body;

// Handle different events
switch (event) {
case 'disconnect':
console.log('User disconnected:', data);
// Add your custom logic here
break;
case 'join':
console.log('User joined:', data);
// Add your custom logic here
break;
case 'exit':
console.log('User exited:', data);
// Add your custom logic here
break;
default:
console.error('Unknown event type');
break;
}

res.status(200).send('Webhook received');
});

// Start the server
app.listen(port, () => {
console.log(`Webhook server running on http://localhost:${port}`);
});

0 comments on commit 57faf15

Please sign in to comment.