Skip to content

Commit

Permalink
[mirotalksfu] - improve webhook, add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Jan 16, 2025
1 parent 9ce98c9 commit 0cf6fda
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 15 deletions.
10 changes: 6 additions & 4 deletions app/src/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ module.exports = class Logger {
);
}

getDateTime() {
const currentTime = new Date().toLocaleString('en-US', this.tzOptions);
const milliseconds = String(new Date().getMilliseconds()).padStart(3, '0');
return colors.cyan(`${currentTime}:${milliseconds}`);
getDateTime(color = true) {
const now = new Date();
const currentTime = now.toLocaleString('en-US', this.tzOptions);
const milliseconds = String(now.getMilliseconds()).padStart(3, '0');
const timestamp = `${currentTime}:${milliseconds}`;
return color ? colors.cyan(timestamp) : timestamp;
}

getFormatTime(ms) {
Expand Down
10 changes: 7 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 [email protected] or purchase directly via CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970
* @author Miroslav Pejic - [email protected]
* @version 1.7.04
* @version 1.7.05
*
*/

Expand Down Expand Up @@ -2843,6 +2843,8 @@ function startServer() {

if (webhook.enabled) {
const data = {
timestamp: log.getDateTime(false),
room_id: socket.room_id,
peer_name: peer_name,
presenter: isPresenter,
reason: reason,
Expand Down Expand Up @@ -2895,12 +2897,12 @@ function startServer() {
log.info('[REMOVE ME] - Last peer - current active RTMP streams', activeStreams);
}

socket.room_id = null;

if (isPresenter) removeIP(socket);

if (webhook.enabled) {
const data = {
timestamp: log.getDateTime(false),
room_id: socket.room_id,
peer_name: peer_name,
presenter: isPresenter,
};
Expand All @@ -2911,6 +2913,8 @@ function startServer() {
.catch((error) => log.error('Error tracking exitRoom event:', error.message));
}

socket.room_id = null;

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

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.04",
"version": "1.7.05",
"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 [email protected] or purchase directly via CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970
* @author Miroslav Pejic - [email protected]
* @version 1.7.04
* @version 1.7.05
*
*/

Expand Down Expand Up @@ -4904,7 +4904,7 @@ function showAbout() {
imageUrl: image.about,
customClass: { image: 'img-about' },
position: 'center',
title: 'WebRTC SFU v1.7.04',
title: 'WebRTC SFU v1.7.05',
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 [email protected] or purchase directly via CodeCanyon
* @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970
* @author Miroslav Pejic - [email protected]
* @version 1.7.04
* @version 1.7.05
*
*/

Expand Down
50 changes: 50 additions & 0 deletions webhook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Webhooks Example

![webhook](./webhooks.png)

This example shows how to set up a server to listen for MiroTalk SFU webhook events (join, exitRoom, disconnect).

### Step 1: Enable Webhooks

Edit `app/src/config.js` to enable webhooks:

```javascript
webhook: {
enabled: true, // Enable webhook functionality
url: 'http://localhost:8888/webhook-endpoint', // Webhook server URL
},
```

---

### Step 2: Run the Webhook Server

1. **Install dependencies**:

```bash
npm install
```

2. **Start the server**:

```bash
npm start
```

---

### Step 3: Webhook Events

MiroTalk SFU sends HTTP `POST` requests to the specified URL with event data:

**Example Payload**:

```json
{
"event": "join",
"data": {}
}
```

- **Events**: `join`, `exit`, `disconnect`.
- **Data**: Includes `event` and custom `data`.
8 changes: 4 additions & 4 deletions webhook/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ app.post('/webhook-endpoint', (req, res) => {

// 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
Expand All @@ -27,6 +23,10 @@ app.post('/webhook-endpoint', (req, res) => {
console.log('User exited:', data);
// Add your custom logic here
break;
case 'disconnect':
console.log('User disconnected:', data);
// Add your custom logic here
break;
default:
console.error('Unknown event type');
break;
Expand Down
Binary file added webhook/webhooks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0cf6fda

Please sign in to comment.