Skip to content

Commit 1e9dac5

Browse files
committed
Examples: handling Qencode callbacks
1 parent 4c7d152 commit 1e9dac5

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

Examples/callback_test/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Callback sample scripts
2+
Example of sending job with callback_url and payload
3+
4+
- testCallback.js: launches a transcoding job specifying a "callback_url" param and payload for the job.
5+
- callback_handler.js: server side script to accept the callback request. The callback_url param for the job should contain a valid HTTP(S) URL where the callback_handler.js script is working.
6+
7+
## Workflow
8+
1. Install prerequisites for callback_handler.js:
9+
```
10+
npm install express body-parser
11+
```
12+
2. Launch callback_handler.js:
13+
```
14+
node callback_handler.js
15+
```
16+
By default it will start the HTTP service on port 3000, the callback url will be http://<your_host>:3000/callback
17+
You can optionally use [ngrok](https://ngrok.com/) tool on localhost to provide you with a public URL:
18+
```
19+
ngrok http 3000
20+
```
21+
ngrok will provide a public URL (e.g., https://abc123.ngrok.io). Use this URL as the callback URL in Qencode's settings.
22+
3. Excute testCallback.js to run a test job. Don't forget to specify your Qencode API Key and your callback url.
23+
24+
You should see notifications on callback events in the callback_handler.js output.
25+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
4+
const app = express();
5+
const port = 3000;
6+
7+
// Middleware to parse URL-encoded bodies (as sent by HTML forms)
8+
app.use(bodyParser.urlencoded({ extended: true }));
9+
10+
// POST endpoint to handle the callback
11+
app.post('/callback', (req, res) => {
12+
// Extracting the task token and payload from the request body
13+
const taskToken = req.body.task_token;
14+
const event = req.body.event;
15+
const payload = req.body.payload ? JSON.parse(req.body.payload) : null;
16+
17+
// Logging the task token and payload
18+
console.log('Received Callback');
19+
console.log('Task Token:', taskToken);
20+
console.log('Event:', event)
21+
if (payload) {
22+
console.log('Payload:', payload);
23+
}
24+
25+
// Sending a response back to the caller
26+
res.send('Callback received');
27+
});
28+
29+
app.listen(port, () => {
30+
console.log(`Server listening at http://localhost:${port}`);
31+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
(async function () {
2+
3+
const QencodeApiClient = require('qencode-api');
4+
5+
const apiKey = "your_api_key_here";
6+
7+
const payload = '{"test": "payload data"}';
8+
9+
let transcodingParams = {
10+
"source": "https://nyc3.s3.qencode.com/qencode/bbb_30s.mp4",
11+
"format": [
12+
{
13+
"output": "metadata"
14+
}
15+
],
16+
"callback_url": "https://abc123.ngrok-free.app/callback"
17+
};
18+
19+
20+
21+
const qencodeApiClient = await new QencodeApiClient(apiKey);
22+
console.log("AccessToken: ", qencodeApiClient.AccessToken);
23+
24+
let task = await qencodeApiClient.CreateTask();
25+
console.log("Created new task: ", task.taskToken);
26+
27+
await task.StartCustom(transcodingParams, payload);
28+
console.log("Status URL: ", task.statusUrl);
29+
30+
31+
// example on how to get status
32+
CheckTaskStatus();
33+
34+
async function CheckTaskStatus(){
35+
let statusObject = await task.GetStatus()
36+
let status = statusObject.status
37+
while (status != "completed") {
38+
statusObject = await task.GetStatus()
39+
status = statusObject.status;
40+
progress = statusObject.percent;
41+
console.log(`task: ${task.taskToken} | status: ${status} | progress: ${progress}`);
42+
await sleep(10000);
43+
}
44+
}
45+
46+
function sleep(ms){
47+
return new Promise(resolve=>{
48+
setTimeout(resolve,ms)
49+
})
50+
}
51+
52+
53+
})();
54+

0 commit comments

Comments
 (0)