0

I have a simple express.js backend that handles a get request from discord which runs a heavy process in the background using bull and throng.

I got it all working, Discord sends the get request. backend reserves the request and adds it to the job queue, once it's done I receive the returned value in the Queue.on() listener. My problem is how can I send this result I get in the queue.on() listener back to discord as a message. how can I make discord listen to this event and post the result? Note: discord and express are 2 separate apps. can't integrate them into one project.

Index.js file:

const express = require('express')
const scrapTikTok = require('./tiktokScraping')
const Queue = require('bull')
const app = express()

// Initialize BULL
const REDIS_URL = process.env.REDIS_URL || 'redis://127.0.0.1:6379'
const workQueue = new Queue('work', REDIS_URL, {
    defaultJobOptions: {
        removeOnComplete: true,
        removeOnFail: true
    }
})

app.get('/scrap', async (req, res) => {
    res.send('Working on it...')
    await scrapTikTok()
})

app.get('/url', async (req, res) => {
    const url = 'https://www.tiktok.com/@tristanvincentt'
    let job = await workQueue.add({url: url})
    res.json({id: job.id})
})


workQueue.on('global:completed', (jobId, result) => {
    console.log(`Job completed with result ${result}`)
    // SEND RESULT BACK TO DISCORD
})
app.listen(process.env.PORT || 3000, () => {
    console.log('App is running...')
})

Worker file:

const throng = require('throng')
const Queue = require("bull")

const REDIS_URL = process.env.REDIS_URL || "redis://127.0.0.1:6379"
const workers = process.env.WEB_CONCURRENCY || 1
const maxJobsPerWorker = 50
const sleep = ms => new Promise(r => setTimeout(r, ms));

function start() {
    const workQueue = new Queue('work', REDIS_URL)

    workQueue.process(maxJobsPerWorker, async job => {
        console.log(job.data.url)
        await sleep(10000)
        return {value: job.id}
    })
}

throng({workers, start})
Alaa
  • 1
  • 1

1 Answers1

0

you can create another express server in your discord app. and add a post endpoint:

const express = require('express')
const app = express()

app.post('/secreturl', async (req, res) => {
    res.send('Discordjs Received the data!')
    let jobId = req.body.jobId
    let result = req.body.result
    //now you have the results here!
})

then when the queue is done (based on the code you provided):

workQueue.on('global:completed', (jobId, result) => {
    console.log(`Job completed with result ${result}`)
    // SEND RESULT BACK TO DISCORD:
    axios.post('(your-discordjs-server-ip)/secreturl', { 
        jobId: jobId, 
        result: result 
    })
})

just make sure you add the axios package to the queue app

npm install axios

and require it in the index.js file

const axios = require("axios")

tick this as the answer if it solved your problem!

Parsa
  • 23
  • 7
  • Thank you @Root for the answer, That was a new idea and I will give it a try. I came up with a way of solving it by using the channel webhook along with the discord-webhook-node library and It did solve my Issue. To be honest, my main point was to learn it and you gave me an idea to try. – Alaa Nov 11 '22 at 20:36
  • @Alaa, yes, thats another way of solving it. but if you wanted to use slash commands or for example send it to a user's dm, that wouldnt work – Parsa Nov 11 '22 at 21:25